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

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

Discord Python:向成員添加角色

Discord Python:向成員添加角色

郎朗坤 2023-03-30 10:12:48
我的機器人會檢查何時將用戶添加到 Discord 上的公會,然后私下向他們發送 DM 以獲取他們的電子郵件地址。然后它會向電子郵件地址發送一個一次性代碼,并要求用戶在 DM 中輸入該代碼。所有這些都已實施并有效。但是,當用戶回答代碼時,我似乎無法為用戶分配新角色。這是我目前擁有的(我刪除了檢查一次性代碼等的代碼,因為它可以工作并且似乎不是問題的根源):import discordfrom discord.ext import commandsfrom discord.utils import [email protected] def on_message(message):    # Check if message was sent by the bot    if message.author == client.user:        return    # Check if the message was a DM    if message.channel.type != discord.ChannelType.private:        return    user_code = 'some code sent via email'    if message.content == user_code:        member = message.author        new_guild = client.get_guild(int(GUILD_ID))        role = get(new_guild.roles, id=DISCORD_ROLE)        await member.add_roles(role)        response = "You can now use the Discord Server."        await message.channel.send(response)這是我收到的錯誤:Traceback (most recent call last):  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 312, in _run_event    await coro(*args, **kwargs)  File "main.py", line 89, in on_message    await member.add_roles(role)AttributeError: 'User' object has no attribute 'add_roles'
查看完整描述

1 回答

?
蝴蝶不菲

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

為此,您需要將User對象轉換為Member對象。這樣,您就可以調用該add_roles方法。這是一種方法:

import discord

from discord.ext import commands

from discord.utils import get


@client.event

async def on_message(message):

? ? # Check if message was sent by the bot

? ? if message.author == client.user:

? ? ? ? return


? ? # Check if the message was a DM

? ? if message.channel.type != discord.ChannelType.private:

? ? ? ? return


? ? user_code = "some code sent via email"


? ? if message.content == user_code:

? ? ? ? new_guild = client.get_guild(int(GUILD_ID))


? ? ? ? member = new_guild.get_member(message.author.id)

? ? ? ? role = new_guild.get_role(int(DISCORD_ROLE))

? ? ? ? await member.add_roles(role)


? ? ? ? response = "You can now use the Discord Server."

? ? ? ? await message.channel.send(response)


查看完整回答
反對 回復 2023-03-30
  • 1 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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