首页 > 解决方案 > 运行令牌 Discord.py

问题描述

import discord
from discord.ext import commands, tasks

tokens = [
    'dgfggg.YJK14Q.rgrgrgrgrgrgrgrgrg-ZjlvsPyQXE',
    'adddawd.YJK3BQ.grsggrgrgrgrgrg-Y',
    'wadwdwad.YJK37g.gfregrgegfegefte4tyg'
]

bot = commands.Bot(command_prefix="//")


for token in tokens:
    print(token)
    bot.run(f"{token}", bot=False)

为什么这项工作不起作用有什么办法可以运行这样的令牌。

标签: pythondiscord

解决方案


asyncio您可以通过创建将同时在同一线程上运行的多个任务来运行多个带有模块的机器人。这是我前段时间写的一个旧脚本:

import asyncio
import discord
from discord.ext import commands

tokens = ['token1', 'token2', 'token3']

class myBot(commands.Bot):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    async def on_ready(self):
        print('bot ready')

try:
    loop = asyncio.get_event_loop()
    tasks = []
    for i, token in enumerate(tokens): # putting all of the bot tokens into tasks
        temp = myBot(command_prefix='//')
        temp.token = token
        try:
            # Using .start function here because .run, .connect, and .login functions all have some complex checks for "connection" I'm not 100% sure what the difference between those functions are, but hey, .start function works fine.
            tasks.append(loop.create_task( temp.start(token) ))
        except Exception as e:
            print(f'unable to login into bot #{str(i)}')
    # run the bots
    gathered = asyncio.gather(*tasks, loop=loop)
    loop.run_until_complete(gathered)

except KeyboardInterrupt:
    print('Exiting...')

注意:您必须确保tokens列表中的所有令牌都有效,或者您可以创建令牌检查器。否则,脚本会抛出一个无效令牌错误。


推荐阅读