【问题标题】:Is there a way to convert a string to a discord.Member type in discord.py?有没有办法将字符串转换为 discord.py 中的 discord.Member 类型?
【发布时间】:2021-05-13 22:38:05
【问题描述】:

我一直在尝试将井字游戏迷你游戏添加到我的不和谐机器人中。我最初在 @client.command() 中编写了代码,因为我认为您可以在其中使用 client.wait_for() 但不幸的是,您不能。无论如何,我必须将我的代码转换为在 on_message() 函数上工作,现在我遇到了从初始命令中获取 discord.Member 变量类型的问题,例如 ~tictactoe @user#1234。 因此,例如,我尝试在 on_message() 函数中编写此代码,但没有成功。

if message.content.startswith("~tictactoe") or message.content.startwith("~ttt"):
    member = str(message).split(" ")
    member = discord.Member(member[1])
    channel = message.channel

这是我的完整代码:

if message.content.startswith("~tictactoe") or message.content.startwith("~ttt"):
    member = str(message).split(" ")
    member = discord.Member(member[1])
    channel = message.channel
    if member.bot: channel.send("You can't play against a bot!")
    else:
        while True:
            x = 0
            player = [message.author if (x % 2 == 0) else member]
            x += 1
            symbol = [':x:' if player == message.author else ':o:']
            def check(m):
                possibilities = ['a1','a2','a3','b1','b2','b3','c1','c2','c3']
                return (m.content.lower() in possibilities or m.content.lower() == 'end') and m.author == player and m.channel == channel
            if x != 1:
                channel.send("TicTacToe\n{message.author.name} vs. {member.name}")
                for i in range(3):
                    channel.send(f"{board[i][0]}{board[i][1]}{board[i][2]}\n")
            channel.send(f"{player.mention}, where do you want to place your marker?\na1\ta2\ta3\nb1\tb2\tb3\nc1\tc2\tc3\n or `end` to end game")
            try:
                cell = await client.wait_for('message', timeout=20.0, check=check)
            except:
                channel.send("Input invalid or you took too long to respond.")
            else:
                cell = cell.lower()
                if cell == 'end':
                    break
                possibilities = [['a1','a2','a3'], ['b1','b2','b3'], ['c1','c2','c3']]
                board = [[':black_large_square:',':black_large_square:',':black_large_square:'], [':black_large_square:',':black_large_square:',':black_large_square:'], [':black_large_square:',':black_large_square:',':black_large_square:']]
                for i in range(3):
                    for j in range(3):
                        if cell == str(possibilities[i][j]): board[i][j] == symbol
                won = False
                for i in range(3):
                    if board[i][0] == board[i][1] == board[i][2] and won == False:
                        channel.send(f"{player} won the game!")
                        won == True
                    if board[0][i] == board[1][i] == board[2][i] and won == False:
                        channel.send(f"{player} won the game!")
                        won = True
                if board[0][0] == board[1][1] == board[2][2] and won == False:
                    channel.send(f"{player} won the game!")
                    won = True
                if board[0][2] == board[1][1] == board[2][0] and won == False:
                    channel.send(f"{player} won the game!")
                    won = True

任何帮助 :)

【问题讨论】:

    标签: python python-3.x discord discord.py discord.py-rewrite


    【解决方案1】:

    使用message.mentions


    message.mentions 返回提到的discord.Member 列表(如果消息是通过私人消息发送的,则返回discord.User)。

    # Safe method
    if message.mentions:
        member = message.mentions[0]
    else:
        return  # There was no mentions
    
    # Riskier but simpler method
    # Having no mentions or more than one would raise an error
    member, = message.mentions
    

    快速提示:回复被视为提及,message.mentions 将包含您的消息中提及的成员以及您回复的成员。

    解析消息


    提及相当于<@!id>,因此您可以解析您的消息以获取成员的id:

    command, member = message.split()
    member_id = int(member.strip('<@!>'))
    

    然后,要从中取出 discord.Member 对象:

    # Regardless of cache, but sends an API call
    member = await bot.fetch_member(member_id)
    
    # Depends on the bot's cache
    # Doesn't make any API calls and isn't asynchronous
    member = message.guild.get_member(member_id)
    

    【讨论】:

    • 当我运行机器人并编写命令时,它给了我这个错误NameError: name 'self' is not defined 我该怎么做才能解决它?附言我尝试了提及的方式
    • 我的错 ^^ 我用我的代码来回答你的问题,但我的机器人变量略有不同。您必须将 self.bot 替换为 botclient,具体取决于您的机器人变量。
    猜你喜欢
    • 2022-06-18
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多