'Discord.py have a command run on one specific channel
So, Basically what I am trying to do is I've been trying to make a bot that takes user input, then tells them if whatever they put in is available or not. I got the randomizer and Availability, and the command works well with the prefix, but when i run it, it's running on other channels as well. IE: I type something in one channel, it makes the message in another channel. What i want is for the user to be able to use the command in ONE channel. Also, the command is running without the prefix as well, is there any reason why? Am I doing something wrong, because I feel like I am. Here's my code for reference:
import discord
from discord import Intents, channel, colour
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from discord import Embed
from discord.ext import commands
from discord.ext.commands import Bot as BotBase
from discord.ext.commands.core import check
from discord.ext.commands.errors import CommandNotFound
from discord.message import Message
from ygoprodeck import *
import random
from ..db import db
PREFIX = "+"
OWNER_IDS = [OWNER_ID]
ygo = YGOPro()
bot = commands.Bot(command_prefix='#')
class Bot(BotBase):
def __init__(self):
self.PREFIX = PREFIX
self.ready = False
self.guild = None
self.scheduler = AsyncIOScheduler()
db.autosave(self.scheduler)
super().__init__(
command_prefix=PREFIX,
owner_ids=OWNER_IDS,
intents=Intents.all(),
)
def run(self, version):
self.VERSION = version
with open("./lib/bot/token.0", "r", encoding="utf-8") as tf:
self.TOKEN = tf.read()
print("Running bot...")
super().run(self.TOKEN, reconnect=True)
async def on_connect(self):
print("Bot Connected")
async def on_disconnect(self):
print("Bot Disconnected")
async def on_error(self, err, *args, **kwargs):
if err == "on_command_error":
await args[0].send("Something went wrong.")
await self.stdout.send("An error occured.")
raise
async def on_command_error(self, ctx, exc):
if isinstance(exc, CommandNotFound):
pass
elif hasattr(exc, "original"):
raise exc.original
else:
raise exc
async def on_ready(self):
if not self.ready:
self.ready = True
self.guild = self.get_guild(GUILD_ID)
self.stdout = self.get_channel(854734087855013908)
self.scheduler.start()
await self.stdout.send("Now Online!")
global result_main
choices = ["yes", "no"]
run = True
while run:
channel = bot.get_channel(854734087855013908)
if channel == channel:
msg = await bot.wait_for("message", )
ygo_card = msg.content[1:]
message = ygo_card
value_random = random.choice(choices)
embed = Embed(title="Card Name:", description=message)
embed.add_field(name="Available: ", value=value_random, inline=False)
await channel.send(embed=embed)
print("Bot Ready")
else:
print("Bot Reconnected")
async def on_message(self, message):
pass
bot = Bot()
Solution 1:[1]
If channel == channel is always true, you probably meant something like self.channel
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | General Grievance |
