第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Discord Python:向成員添加角色

Discord Python:向成員添加角色

郎朗坤 2023-03-30 10:12:48
我的機(jī)器人會檢查何時(shí)將用戶添加到 Discord 上的公會,然后私下向他們發(fā)送 DM 以獲取他們的電子郵件地址。然后它會向電子郵件地址發(fā)送一個(gè)一次性代碼,并要求用戶在 DM 中輸入該代碼。所有這些都已實(shí)施并有效。但是,當(dāng)用戶回答代碼時(shí),我似乎無法為用戶分配新角色。這是我目前擁有的(我刪除了檢查一次性代碼等的代碼,因?yàn)樗梢怨ぷ鞑⑶宜坪醪皇菃栴}的根源):import discordfrom discord.ext import commandsfrom discord.utils import get@client.eventasync 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)這是我收到的錯(cuò)誤: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貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

為此,您需要將User對象轉(zhuǎn)換為Member對象。這樣,您就可以調(diào)用該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)


查看完整回答
反對 回復(fù) 2023-03-30
  • 1 回答
  • 0 關(guān)注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號