亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我將如何著手制作一個允許服務器管理員阻止機器人在指定頻道中響應的命令?

我將如何著手制作一個允許服務器管理員阻止機器人在指定頻道中響應的命令?

慕碼人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 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號