簡體   English   中英

massban / massunban 命令僅適用於服務器內的用戶。 我怎樣才能使它也禁止/取消禁止不在服務器中的用戶?

[英]massban / massunban commands only work for users within the server. How can I make it so it also bans/unbans users not in the server?

所以我一直在嘗試創建兩個命令——一個禁止多個用戶,另一個取消多個用戶的禁令;massban 和 massunban。

這兩個命令對服務器中的用戶有效,需要注意的是它不適用於不在服務器中的 ID。

這里的思路是做.massban ID ID ID ID ID(...) 和.massunban ID ID ID ID(...)

    @commands.command(name="massban")
    @commands.has_any_role("Admin", "Moderator", "Bot")
    async def massban(self, context, *user_ids: int):
        for u in user_ids:
            user_object = self.client.get_user(u)
            await context.guild.ban(user_object)

    @commands.command(name="massunban")
    @commands.has_any_role("Admin", "Moderator", "Bot")
    async def massunban(self, context, *user_ids: int):
        for u in user_ids:
            user_object = self.client.get_user(u)
            await context.guild.unban(user_object)

問題是這會返回如下錯誤

上述異常是以下異常的直接原因:

Traceback (most recent call last):
  File "C:\Users\Dazz\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Dazz\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Dazz\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'id'

我一直在查看文檔,到處尋找解決此問題的方法; 我在幾台 Discord 服務器上尋求幫助,但無濟於事。

任何幫助,將不勝感激。

問題是,用戶不會自動加載到緩存中。 所以 Discord.py 無法從緩存中獲取用戶。 這里的解決方案是使用.fetch_user() 這是一個 API 調用,您不應該使用它,但在這種情況下您必須使用它。

所以修改后的代碼如下:

@commands.command(name="massban")
@commands.has_any_role("Admin", "Moderator", "Bot")
async def massban(self, context, *user_ids: int):
    for u in user_ids:
        user_object = await self.client.fetch_user(u)
        await context.guild.ban(user_object)

@commands.command(name="massunban")
@commands.has_any_role("Admin", "Moderator", "Bot")
async def massunban(self, context, *user_ids: int):
    for u in user_ids:
        user_object = await self.client.fetch_user(u)
        await context.guild.unban(user_object)

添加到 FlexGames 答案中。 1.6版中,discord.py UserConverter已經獲取了如果您提供了 ID 並檢查 ID 是否有效的用戶。 答案可以縮短。 UserConverter 還接受名稱/提及/ID,這是一個獎勵。 這里說明了UserConverter

這是精簡版

@commands.command(name="massban")
@commands.has_any_role("Admin", "Moderator", "Bot")
async def massban(self, ctx, *users: discord.User):
    # map calls ctx.guild.ban for each users
    # asyncio.gather awaits each coroutine concurrently
    await asyncio.gather(*map(ctx.guild.ban, users))

@commands.command(name="massunban")
@commands.has_any_role("Admin", "Moderator", "Bot")
async def massunban(self, ctx, *users: discord.User):
    await asyncio.gather(*map(ctx.guild.unban, users))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM