为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕码人2483693 2023-06-13 14:54:24
我的机器人现在在几台服务器上,我得到的主要反馈之一是服务器管理员想阻止机器人在某些频道中做出响应,而无需通过 Discord 的权限管理器。但是我不确定从哪里开始,所以我想我会在这里伸出援手,看看我是否可以获得任何建议或代码片段以供使用!基本上管理员会使用 like!fg ignore 'channel name or id'然后机器人会在某个地方存储它并且不响应,然后类似地如果他们使用!fg unignore 'channel name or id'它然后将其从列表或它存储的地方删除。任何帮助将不胜感激,谢谢!
查看完整描述

2 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

这是我为使其正常工作而制作的示例:


import discord

from discord.ext import commands


ignoredChannels = [] # List of all the ignored channels, you can use a text file instead if you prefer


client = discord.ext.commands.Bot(command_prefix = "fg!"); 


@client.event

async 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 removed



client.run(your_bot_token) # Run the bot with your token

它是如何工作的,每次发送消息时,它都会检查列表中是否存在频道 ID,如果它在列表中找到该频道,它将返回,否则什么都不做,如果该频道不在列表中,它将继续处理该通道中的命令。


如果您只想允许管理员使用您可以@commands.has_permissions(administrator=True)在每一行下添加的命令@client.command()。


希望它有所帮助并祝您编码愉快:)


查看完整回答
反对 回复 2023-06-13
?
慕码人8056858

TA贡献1803条经验 获得超6个赞

您需要将频道 ID 保存在列表中,然后在机器人 on_message 函数中检查消息是否不在该频道中,如果不在则运行您的命令。



查看完整回答
反对 回复 2023-06-13
  • 2 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信