我将如何着手制作一个允许服务器管理员阻止机器人在指定频道中响应的命令?

我的机器人现在在几台服务器上,我得到的主要反馈之一是服务器管理员想阻止机器人在某些频道中做出响应,而无需通过 Discord 的权限管理器。但是我不确定从哪里开始,所以我想我会在这里伸出援手,看看我是否可以获得任何建议或代码片段以供使用!

基本上管理员会使用 like!fg ignore 'channel name or id'然后机器人会在某个地方存储它并且不响应,然后类似地如果他们使用!fg unignore 'channel name or id'它然后将其从列表或它存储的地方删除。

任何帮助将不胜感激,谢谢!


慕码人2483693
浏览 81回答 2
2回答

海绵宝宝撒

这是我为使其正常工作而制作的示例:import discordfrom discord.ext import commandsignoredChannels = [] # List of all the ignored channels, you can use a text file instead if you preferclient = discord.ext.commands.Bot(command_prefix = "fg!"); @client.eventasync def on_message(message):    if message.channel.id in ignoredChannels:        return # If the channel is in the ignored list - return    else:        await client.process_commands(message) # Otherwise process the commands@client.command()async def ignore(ctx, channelID):    if int(channelID) not in ignoredChannels:        ignoredChannels.append(int(channelID)) # Add the channel if it hasn't been added yet        await ctx.send("Successfully added the channel to the ignored list!")    else:        await ctx.send("Channel was already inside the ignored list!") # Otherwise warn user that the channel is already ignored@client.command()async def unignore(ctx, channelID):    try:        ignoredChannels.remove(int(channelID)) # Attempt to remove the channel from the list        await ctx.send("Successfully removed the channel from the ignored list!")    except:        await ctx.send("This channel is already removed!") # If fails, warn the user that the channel is already removedclient.run(your_bot_token) # Run the bot with your token它是如何工作的,每次发送消息时,它都会检查列表中是否存在频道 ID,如果它在列表中找到该频道,它将返回,否则什么都不做,如果该频道不在列表中,它将继续处理该通道中的命令。如果您只想允许管理员使用您可以@commands.has_permissions(administrator=True)在每一行下添加的命令@client.command()。希望它有所帮助并祝您编码愉快:)

慕码人8056858

您需要将频道 ID 保存在列表中,然后在机器人 on_message 函数中检查消息是否不在该频道中,如果不在则运行您的命令。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python