【问题标题】:Discord.py have a command run on one specific channelDiscord.py 在一个特定通道上运行命令
【发布时间】:2021-09-01 11:49:26
【问题描述】:

所以,基本上我想做的是我一直在尝试制作一个接受用户输入的机器人,然后告诉他们他们输入的任何内容是否可用。我得到了随机发生器和可用性,该命令与前缀配合得很好,但是当我运行它时,它也在其他通道上运行。 IE:我在一个频道中输入一些内容,它在另一个频道中生成消息。我想要的是让用户能够在一个频道中使用该命令。此外,该命令也在没有前缀的情况下运行,有什么原因吗?我做错了什么,因为我觉得我是。这是我的代码供参考:

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()

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    如果 channel == channel 始终为真,那么您可能指的是 self.channel 之类的东西

    (如果合适,请接受这个答案,我需要声誉:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-07
      • 2021-11-18
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多