Discord.py bot 不接受异步函数中的变量
Discord.py bot is not accepting variables in async function
我正在尝试编写一个机器人,它从 mee6 的 API 中获取数据,并为排行榜中的前 3 名提供版主角色。我也得到了那些人的用户 ID,但现在我无法将这些用户放入参数中并赋予这些用户角色。
我尝试使用一些事件来做到这一点,但我没有找到合适的消息事件。如果您对此有任何其他解决方案,那将非常有帮助。
我是初学者 Python 用户,我不知道如何有效地使用 API 或编写好的 Python 代码。
代码:
import requests
import json
import discord
from discord.ext import commands, tasks
from discord.utils import get
class applicable_members:
#Variables
member={}
mee6API = "https://mee6.xyz/api/plugins/levels/leaderboard/766213304141086731?limit=999&page=0"
#Gets the data from api
result = requests.get(mee6API).json()
#Gets specific data related to members
players = result.get("players")
#Loop to form dict of player and their xp
for player in players:
member[player.get("xp")] = player.get("id")
#List of user-id of people illegible to get Mod role
people = list(member.values())[:3]
class samrid_bot:
#Variables
token = "<TOKEN>"
role_name = "Mods"
peoples = applicable_members.people
#Declaring Prefix
client = commands.Bot(command_prefix=".", case_insensitive=True)
@client.event
async def on_ready():
print("Bot is ready")
ppl = peoples[1]
@tasks.loop(seconds=5.0, count=5)
async def addrole(person, role_n):
member = person
role = role_n
await client.add_roles(member, role)
addrole.start(ppl, role_name)
client.run(token)
错误:
Unhandled exception in internal background task 'add'.
Traceback (most recent call last):
File "C:\Users\samri\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\tasks\__init__.py", line 101, in _loop
await self.coro(*args, **kwargs)
File "c:/Users/samri/OneDrive - Hoffman/Documents/Websites/Discord Bot/Samrids Bot/bot.py", line 52, in add
await client.add_roles(member, role)
AttributeError: 'Bot' object has no attribute 'add_roles'
Bot is ready
on_message
是一个只接受一个参数的事件监听器,message
。 https://discordpy.readthedocs.io/en/latest/api.html#discord.on_message
每当发送消息时执行,消息参数设置为发送的消息。您不能修改它以采用您自己的参数,这就是该代码失败的原因。
编辑:为了重复它,你最好使用 discord.py 提供的任务循环 https://discordpy.readthedocs.io/en/latest/ext/tasks/
我正在尝试编写一个机器人,它从 mee6 的 API 中获取数据,并为排行榜中的前 3 名提供版主角色。我也得到了那些人的用户 ID,但现在我无法将这些用户放入参数中并赋予这些用户角色。
我尝试使用一些事件来做到这一点,但我没有找到合适的消息事件。如果您对此有任何其他解决方案,那将非常有帮助。
我是初学者 Python 用户,我不知道如何有效地使用 API 或编写好的 Python 代码。
代码:
import requests
import json
import discord
from discord.ext import commands, tasks
from discord.utils import get
class applicable_members:
#Variables
member={}
mee6API = "https://mee6.xyz/api/plugins/levels/leaderboard/766213304141086731?limit=999&page=0"
#Gets the data from api
result = requests.get(mee6API).json()
#Gets specific data related to members
players = result.get("players")
#Loop to form dict of player and their xp
for player in players:
member[player.get("xp")] = player.get("id")
#List of user-id of people illegible to get Mod role
people = list(member.values())[:3]
class samrid_bot:
#Variables
token = "<TOKEN>"
role_name = "Mods"
peoples = applicable_members.people
#Declaring Prefix
client = commands.Bot(command_prefix=".", case_insensitive=True)
@client.event
async def on_ready():
print("Bot is ready")
ppl = peoples[1]
@tasks.loop(seconds=5.0, count=5)
async def addrole(person, role_n):
member = person
role = role_n
await client.add_roles(member, role)
addrole.start(ppl, role_name)
client.run(token)
错误:
Unhandled exception in internal background task 'add'.
Traceback (most recent call last):
File "C:\Users\samri\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\tasks\__init__.py", line 101, in _loop
await self.coro(*args, **kwargs)
File "c:/Users/samri/OneDrive - Hoffman/Documents/Websites/Discord Bot/Samrids Bot/bot.py", line 52, in add
await client.add_roles(member, role)
AttributeError: 'Bot' object has no attribute 'add_roles'
Bot is ready
on_message
是一个只接受一个参数的事件监听器,message
。 https://discordpy.readthedocs.io/en/latest/api.html#discord.on_message
每当发送消息时执行,消息参数设置为发送的消息。您不能修改它以采用您自己的参数,这就是该代码失败的原因。
编辑:为了重复它,你最好使用 discord.py 提供的任务循环 https://discordpy.readthedocs.io/en/latest/ext/tasks/