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

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

踢命令(discord.py)

踢命令(discord.py)

米脂 2023-09-19 14:51:43
所以我試圖制作一個踢命令,以便如果沒有任何原因,那么它會說“沒有原因”而不是“沒有”。別問為什么。這是我的代碼:@client.command()@commands.has_permissions(kick_members=True)async def kick(ctx, user: discord.Member, *, reason: str):  if reason is None:    await user.kick()    await ctx.send(f"**{user}** has been kicked for **no reason**.")  else:    await user.kick(reason=reason)    await ctx.send(f"**{user}** has been kicked for **{reason}**.")這是錯誤:Ignoring exception in command kick:Traceback (most recent call last):  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke    await ctx.command.invoke(ctx)  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 847, in invoke    await self.prepare(ctx)  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 784, in prepare    await self._parse_arguments(ctx)  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 699, in _parse_arguments    kwargs[name] = await self.transform(ctx, param)  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 535, in transform    raise MissingRequiredArgument(param)discord.ext.commands.errors.MissingRequiredArgument: reason is a required argument that is missing.我不明白為什么它說“原因是缺少的必需參數”,因為我說如果原因是 None 它會說沒有原因?
查看完整描述

3 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

如果您分配None給reason,那么您可以檢查它。例如reason = None。之后,您可以檢查命令是否原因為None。這是代碼:


@client.command()

@commands.has_permissions(kick_members=True)

async def kick(ctx, user: discord.Member, *, reason = None):

  if not reason:

    await user.kick()

    await ctx.send(f"**{user}** has been kicked for **no reason**.")

  else:

    await user.kick(reason=reason)

    await ctx.send(f"**{user}** has been kicked for **{reason}**.")


查看完整回答
反對 回復 2023-09-19
?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

很簡單,只需寫:


@client.command()

@commands.has_permissions(kick_members=True)

    async def kick(self, ctx, user: discord.Member, *, reason=None):

        await user.kick(reason=reason)

        await ctx.send(f'{user.mention} has been kicked for {reason}!')


查看完整回答
反對 回復 2023-09-19
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

您收到該錯誤是因為您的函數如下所示:

async def kick(ctx, user: discord.Member, *, reason: str):

Reason 在這里不是可選的,因此它是一個required argument. 這意味著在沒有該參數的情況下調用此函數將導致錯誤。添加默認值使其成為可選。

def function(requiredArgument, optionalArgument=defaultValue)

在這種情況下,defaultValue應該是None. 現在,當您不為該參數傳遞任何內容時,將使用它的默認值。這樣,您就不再需要添加理由。


查看完整回答
反對 回復 2023-09-19
  • 3 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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