首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不和谐的机器人命令在没有提到的情况下使用随机的提及。有问题

不和谐的机器人命令在没有提到的情况下使用随机的提及。有问题
EN

Stack Overflow用户
提问于 2022-01-09 21:25:37
回答 2查看 106关注 0票数 0

对于python来说有点陌生,而且让它开始工作也变得毫无意义。在输入命令时,我在第23行中没有提到以下错误:

'NoneType‘对象没有属性’

我不知道为什么它试图使用这个响应部分作为它的时候,当有一个提到,无用的我错过了什么。

代码语言:javascript
复制
import discord
from redbot.core import commands
import random

class boop(commands.Cog):
    """My custom cog"""

    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def boop(self, ctx, user: discord.User=None):
        """Boop Someone"""
        # Your code will go here
        author_name = ctx.author.mention
        member = random.choice(ctx.guild.members)
        
        randomuser = [f"{author_name} booped {member.mention}",
                     f"{author_name} booped {member.mention} on the nose",
                     f"{author_name} booped {member.mention}'s snoot",
                     f"{author_name} gave {member.mention} a boop"]
                     
        mentionuser = [f"{author_name} booped {user.mention}",
                     f"{author_name} booped {user.mention} on the nose",
                     f"{author_name} booped {user.mention}'s snoot",
                     f"{author_name} gave {user.mention} a boop"]
        if not user:        
          await ctx.send(random.choice(randomuser))
        else:
          await ctx.send(random.choice(mentionuser))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-09 21:42:48

运行命令时,user变为None,因为这是函数定义中设置的参数的默认值。

然后:

代码语言:javascript
复制
mentionuser = [f"{author_name} booped {user.mention}",
                     f"{author_name} booped {user.mention} on the nose",
                     f"{author_name} booped {user.mention}'s snoot",
                     f"{author_name} gave {user.mention} a boop"]

您正在尝试检索mention属性的NoneType。这就是错误产生的原因。要避免这种情况,请将mentionuser定义包括在else块中:

代码语言:javascript
复制
...
else:
  mentionuser = [f"{author_name} booped {user.mention}",
                       f"{author_name} booped {user.mention} on the nose",
                       f"{author_name} booped {user.mention}'s snoot",
                       f"{author_name} gave {user.mention} a boop"]
  await ctx.send(random.choice(mentionuser))
票数 0
EN

Stack Overflow用户

发布于 2022-01-10 15:04:24

代码语言:javascript
复制
@bot.command()
async def boop(ctx, target:discord.Member=None):
  author=ctx.author.mention
  if not target:
    members=ctx.guild.members
    target=random.choice(members)
  target=target.mention
  responses=[f'{author} has booped {target}!', f'{target}, {author} has booped you!']
  await ctx.send(random.choice(responses))

这对我有用,但我不使用齿轮,我的客户电话也不一样。我认为您的问题在于您使用的是用户对象而不是成员。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70645651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档