所以我有一个机器人,基于Discord.py编写的,我遇到了一个非常奇怪的问题,我的机器人托管在CentOS 6,64位,并安装了python 3.6.3。我通过cd'ing启动机器人到主文件夹并使用python3.6main.py。请参阅此代码片段:
@bot.command(aliases=["pokemon", "Pokemon", "POKEMON", "PoGo", "POGO", "PokeMonGo", "Pokémon GO"], pass_context=True)
async def pokemongo(ctx):
gotrole = discord.utils.get(ctx.message.server.roles, name="1")
pogorole = discord.utils.get(ctx.message.server.roles, name="Pokémon GO")
if gotrole in ctx.message.author.roles:
await bot.say("You already have a role! Contact support if you want to change!")
return
else:
await bot.add_roles(ctx.message.author, pogorole)
time.sleep(2)
await bot.add_roles(ctx.message.author, gotrole)
await bot.say("Got you the role, Pokémon GO!")
这应该完全可行,发生的情况是用户获得角色Pokemon GO,然后角色1,然后奇怪的是,角色Pokemon GO被删除,有时会发生,有时不会,这与角色,权限,之间或下面的代码无关。此片段也用于各种其他角色,使用相同的模板,只是命令名称(async def)和角色变量(在本例中为pogorole)不同
奇怪的部分是完全随机的,实际上是由机器人完成的,而不是其他人,参见下面的导入库
import discord
from discord.ext import commands
from discord.utils import get
import os
import random
import sys
import asyncio
import aiohttp
import time
import psutil
以及使用相同模板的另一个代码片段的另一个示例:
@bot.command(pass_context=True)
async def fortnite(ctx):
gotrole = discord.utils.get(ctx.message.server.roles, name="1")
fortniterole = discord.utils.get(ctx.message.server.roles, name="Fortnite")
if gotrole in ctx.message.author.roles:
await bot.say("You already have a role! Contact support if you want to change!")
return
else:
await bot.add_roles(ctx.message.author, fortniterole)
time.sleep(2)
await bot.add_roles(ctx.message.author, gotrole)
await bot.say("Got you the role, fortnite!")
它不会出错,角色1不会改变或从用户中删除,它只是随机的游戏角色,它与互联网或类似的东西没有任何关系,我真的希望对此有一个解释,并且真的很想听到一些!
干杯,致命
尝试一次添加所有角色。
await bot.add_roles(ctx.message.author, fortniterole, gotrole)
或者尝试使用asyncio.睡眠
而不是time.睡眠
。time.睡眠
完全阻止机器人,因为它不是异步的
await bot.add_roles(ctx.message.author, fortniterole)
await asyncio.sleep(2)
await bot.add_roles(ctx.message.author, gotrole)