【发布时间】: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