[英]Confused on how to make discord bot send a certain amount of messages?
I'm trying to make a small spambot with discord.js, as one of my first python bots.我正在尝试使用 discord.js 制作一个小型垃圾邮件机器人,作为我的第一个 python 机器人之一。 It honestly works fine, but there's one problem.老实说,它运行良好,但有一个问题。 The bot is supposed to spam every channel in a server, but it only sends one message.该机器人应该向服务器中的每个频道发送垃圾邮件,但它只发送一条消息。 I don't know python that well, and I know where the problem is and what I need to do to fix it, I just don't know how to fix it.我不太了解python,我知道问题出在哪里以及我需要做什么来修复它,我只是不知道如何修复它。 I'm guessing I need to put an amount of messages that I want the bot to send, but I don't know how to do that.我猜我需要放置一些我希望机器人发送的消息,但我不知道该怎么做。 I'm hoping someone can help!我希望有人可以提供帮助! (PS I'm not using the bot for anything bad, I just wanna test it out.) Anyway, here's my code: (PS 我不会使用机器人做任何坏事,我只是想测试一下。)无论如何,这是我的代码:
@bot.command()
async def sall(ctx, *, message=None):
if message == None:
for channel in ctx.guild.channels:
try:
await channel.send(random.choice(spam_messages))
except discord.Forbidden:
print(f"{C.RED}Spam Error {C.WHITE}[Cannot send messages]")
return
except:
pass
else:
for channel in ctx.guild.channels:
try:
await channel.send(message)
except discord.Forbidden:
print(f"{C.RED}Sall Error {C.WHITE}[Cannot send messages]")
return
except:
pass
Well, you really only send one message!嗯,你真的只发一条消息!
@bot.command()
@bot.is_owner()
async def sall(ctx, *, message=None):
if message == None:
for channel in ctx.guild.channels:
try:
for i in range(10):
await channel.send(random.choice(spam_messages))
except discord.Forbidden:
print(f"{C.RED}Spam Error {C.WHITE}[Cannot send messages]")
return
except:
pass
else:
for channel in ctx.guild.channels:
try:
for i in range(10):
await channel.send(message)
except discord.Forbidden:
print(f"{C.RED}Sall Error {C.WHITE}[Cannot send messages]")
return
except:
pass
It should work like this.它应该像这样工作。 I added a for i in range(10)
, what it does is, it repeats this 10 times.我添加了一个for i in range(10)
,它的作用是,它重复了 10 次。 So just change it to number of times you want it to send a message.因此,只需将其更改为您希望它发送消息的次数即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.