如何用Python实现读取tokens.txt中多个Discord Bot令牌批量登录功能
Discord多Bot令牌批量登录Python脚本实现
前置准备
- 安装依赖库:执行命令
pip install discord.py - 在脚本同级目录新建
tokens.txt文件,每个Discord Bot令牌单独占一行,不要添加多余空格、空行或其他符号。
完整实现代码
import asyncio import discord from discord.ext import commands # 读取所有令牌 def load_tokens(): try: with open("tokens.txt", "r", encoding="utf-8") as f: tokens = [line.strip() for line in f if line.strip()] return tokens except FileNotFoundError: print("错误:未找到tokens.txt文件,请检查文件是否放在脚本同级目录") exit(1) # 单个Bot启动逻辑 async def run_bot(token): # 配置Bot Intents,可根据你的实际权限需求调整 intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix="!", intents=intents) @bot.event async def on_ready(): print(f"Connected : {bot.user.name}") try: await bot.start(token) except Exception as e: print(f"令牌 {token[:10]}... 登录失败:{str(e)}") # 主函数批量启动所有Bot async def main(): tokens = load_tokens() tasks = [] for token in tokens: tasks.append(run_bot(token)) await asyncio.gather(*tasks) if __name__ == "__main__": asyncio.run(main())
注意事项
- 你需要提前在Discord开发者后台开启对应Bot的
Privileged Gateway Intents权限,和代码里的Intents配置保持一致。 - 单个IP同时登录过多Bot可能触发Discord的限流机制,建议同时登录的Bot数量不要超过10个。
- 如果出现登录失败的提示,检查对应令牌是否有效、网络是否可以正常访问Discord服务。
内容的提问来源于stack exchange,提问作者Insane
相关产品推荐
相关产品推荐

