【问题标题】:Run 10 bots with 1 script without rewrite 10 times the code (discord.py)使用 1 个脚本运行 10 个机器人,无需重写 10 次代码 (discord.py)
【发布时间】:2020-11-11 22:50:38
【问题描述】:

我在我的 discord 服务器中创建了 10 个 discord 机器人来玩,我想为每个机器人创建相同的功能,但是为了编写一个简单的 bot 命令我需要编写 40 行代码,有什么方法可以获取代码更短?因为同样的代码写10次是很无聊的。这是我的代码。

import asyncio
import random
import discord
from discord.ext import commands

bot_1 = commands.Bot(command_prefix='()')
bot_2 = commands.Bot(command_prefix='()')
bot_3 = commands.Bot(command_prefix='()')
bot_4 = commands.Bot(command_prefix='()')
bot_5 = commands.Bot(command_prefix='()')
bot_6 = commands.Bot(command_prefix='()')
bot_7 = commands.Bot(command_prefix='()')
bot_8 = commands.Bot(command_prefix='()')
bot_9 = commands.Bot(command_prefix='()')
bot_10 = commands.Bot(command_prefix='()')


loop = asyncio.get_event_loop()

loop.create_task(bot_1.start("token 1"))
loop.create_task(bot_2.start("token 2"))
loop.create_task(bot_3.start("token 3"))
loop.create_task(bot_4.start("token 4"))
loop.create_task(bot_5.start("token 5"))
loop.create_task(bot_6.start("token 6"))
loop.create_task(bot_7.start("token 7"))
loop.create_task(bot_8.start("token 8"))
loop.create_task(bot_9.start("token 9"))
loop.create_task(bot_10.start("token 10"))

@bot_1.event
async def on_ready():
    print("Tutti i bot sono stati caricati correttamente.\n")


@bot_1.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")
@bot_2.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")
@bot_3.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")
@bot_4.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")
@bot_5.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")
@bot_6.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")
@bot_7.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")
@bot_8.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")
@bot_9.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")
@bot_10.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")


try:
    loop.run_forever()
finally:
    loop.stop()

(我不能发布这个因为主要是代码)(我不能发布这个因为主要是代码)(我不能发布这个因为主要是代码)(我不能发布这个因为主要是代码)(我不能发布这个,因为主要是代码)(我不能发布这个,因为主要是代码)(我不能发布这个,因为主要是代码)(我不能发布这个,因为主要是代码)(我不能发布这个因为主要是代码)(我不能发布这个因为主要是代码)(我不能发布这个因为主要是代码)(我不能发布这个因为主要是代码)

【问题讨论】:

  • 为什么需要 10 个机器人来做同样的事情?顺便说一句,只需使用一个循环。
  • 为了好玩,比如移动播放器、垃圾邮件或播放音乐。
  • 但据我所知,您不能将同一个机器人多次添加到同一个服务器。
  • 我使用了 10 个不同的机器人(带有 10 个不同的令牌)它可以工作
  • 然后创建一个机器人,并使用不同的代码循环调用它们。使用 for 循环和列表。

标签: python python-3.x windows discord discord.py


【解决方案1】:

这可能看起来像

import asyncio
import random
import discord
from discord.ext import commands

TOKENS = ["token 1"
          "token 2"
          "token 3"
          "token 4"
          "token 5"
          "token 6"
          "token 7"
          "token 8"
          "token 9"
          "token 10"]

async def on_ready():
    print("Tutti i bot sono stati caricati correttamente.\n")

async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")

loop = asyncio.get_event_loop()

for token in TOKENS:
    bot = commands.Bot(command_prefix='()')
    loop.create_task(bot.start(token))
    bot.event(on_ready)
    bot.command()(parola_casuale)

try:
    loop.run_forever()
finally:
    loop.stop()

【讨论】:

  • 感谢您的帮助,现在代码可以成功运行。
【解决方案2】:

使用循环和列表:

import asyncio
import random
import discord
from discord.ext import commands

bots = [] #empty
tokens = ["key1","key2"] #here should be all the keys

loop = asyncio.get_event_loop()

@bot.event
async def on_ready():
    print("Tutti i bot sono stati caricati correttamente.\n")


@bot.command()
async def parola_casuale(ctx):
    file = open("Parole.txt", "r")
    parole = file.readlines()
    parola = random.choice(parole)
    await ctx.message.channel.send(f"Ecco a te: {parola}")

for i in range(len(tokens)): #for every token
    bots.append(commands.Bot(command_prefix='()')) #create a bot
    loop.create_task(bot[i].start(tokens[i])) #start the bot

【讨论】:

  • loop.create_task(bot[i].start(tokens[i])) 返回语法错误。为什么?
  • 能否请您发布错误代码?由于缺少令牌,我无法真正复制您的代码。
  • 它返回此错误:Traceback(最近一次调用最后一次):文件“(我的目录)”,第 22 行,在 @bot.event NameError: name 'bot' is not defined
猜你喜欢
  • 2020-12-23
  • 1970-01-01
  • 2015-04-09
  • 2022-01-11
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 2022-11-12
  • 2021-08-10
相关资源
最近更新 更多