我当前正在尝试创建一个discord机器人,在该机器人中,当您在所需通道中键入命令!purge
时,它将不断删除所有发送的消息,而再次键入时,它将停止删除所有已发送消息。消息。
通过在线学习,我知道我必须使用Cog侦听器,但是我不完全知道如何使用命令“触发”它。我需要@commands.Cog.listener()
段落才能使用on_message
侦听器,但是我也无法弄清楚如何删除执行命令!purge
的通道中的消息。
我已经尝试过在键入命令时使用布尔值来打开和关闭,它将使用while循环不断删除消息,但是while循环将停止。可能是因为该命令的会话已过期,尽管不确定。
关于如何使用该功能的任何想法?更具体地说,我如何以某种方式将Cog链接到命令?谢谢!
(我曾经尝试在此处修改我的代码,但由于不相关而将其删除)
我发现的答案 0 :(得分:0)
您必须创建一个触发命令,以启用或禁用清除模式,然后在on_message
函数中,您必须检查清除模式是否已启用。
以下是操作方法(在嵌齿轮内): 由海报编辑
from discord.ext import commands
class Purge_Cog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.channels = {}
@commands.command()
async def purge(self, ctx):
try:
if self.channels[ctx.channel]:
self.channels[ctx.channel] = False
await ctx.channel.send("Purge mode disabled!")
else:
self.channels[ctx.channel] = True
await ctx.channel.send("Purge mode enabled!")
except:
self.channels[ctx.channel] = True
await ctx.channel.send("Purge mode enabled!")
@commands.Cog.listener()
async def on_message(self, message):
try:
if self.channels[message.channel]:
await message.channel.purge(limit=999)
except:
pass
def setup(bot):
bot.add_cog(Purge_Cog(bot))
import discord
from discord.ext import commands
initial_extensions = ['cogs.cogs']
if __name__ == '__main__':
for extension in initial_extensions:
bot.load_extension(extension)
bot.run('Your token')