python - 运行令牌 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)
为什么这项工作不起作用有什么办法可以运行这样的令牌。
解决方案
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
列表中的所有令牌都有效,或者您可以创建令牌检查器。否则,脚本会抛出一个无效令牌错误。
推荐阅读
- python-3.x - 在 Visual Studio Code 中无法选择虚拟环境
- c++ - 在一组集合中搜索的复杂性 (C++)
- python - 如何在基于无监督方面的情感分析中进行主题检测
- firebase - 如何在 Firebase 中过滤并导入谷歌表格
- laravel - 我需要设置一个 Laravel 实例来使用子域运行另一个应用程序
- python - Python3:无法打开文件'sherlock.py' [Errno 2] 没有这样的文件或目录
- c++ - glibc 检测到 *** ./testArray: free(): invalid next size (fast): 0x00000000
- python - pathlib 使用 glob 查找具有给定词干的所有文件
- mysql - Janusgraph 支持 MySQL 或 AWS RDS
- javascript - 如何确定活动 WebGL 上下文的数量?