分配后角色被删除

Role gets removed after assigning

所以我有一个基于 Discord.py 编写的机器人, 我遇到了一个非常奇怪的问题,我的机器人托管在 CentOs 6、64 位上,并安装了 python 3.6.3。 我通过将机器人 cd 到主文件夹并使用 python3.6 main.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.sleep 而不是 time.sleeptime.sleep 完全阻止机器人,因为它不是异步的

await bot.add_roles(ctx.message.author, fortniterole)
await asyncio.sleep(2)
await bot.add_roles(ctx.message.author, gotrole)