由于数学原因,私人不和谐机器人播音员没有发送?

时间:2021-05-29 21:11:06

标签: python discord.py

我拥有 2 台可供公众使用的开放服务器,由于我有 2 台私人员工服务器,我需要快速发布公告!机器人的重点是执行 .announce (message) 命令,它将发送给所有 私人服务器中的人。但是它不起作用,我不知道它是否与我的数学方程式有关。不确定是 += x 还是 = +x。也可能是 if not member.bot,我不太确定我在做什么。

from discord.ext import commands
from discord.utils import get

token = 'token'

client = commands.Bot(command_prefix = '.')
client.remove_command('help')

@client.event
async def on_ready():
    await client.change_presence(activity=discord.Streaming(name="Watching Your Moderation", url='https://www.twitch.tv/discord'))
    print('online')
        
@client.event
async def on_command_error(ctx, error):
    if hasattr(ctx.command, "on_error"):
        return
    if isinstance(error, commands.CommandNotFound):
        return


@client.command()
async def announce(ctx, *, content):
    if ctx.author.id == ownerid:
        await ctx.send("Starting messaging.")
        not_sent = 0
        total_messaged = 0
        for guild in client.guilds:
            for member in guild.members:
                if not member.bot:
                    try:
                        await member.send(content)
                        total_messaged = +1
                    except discord.HTTPException:
                        not_sent = +1
        await ctx.send(f"{total_messaged} have been messaged. {not_sent} have not been messaged (not enough permissions).")

client.run(token)```

1 个答案:

答案 0 :(得分:1)

你的问题是变量的分配

variable = +1

不增加它的值,只是将它的值设置+1,基本等于1


增加一个变量(在这种情况下你想要的)正在使用

variable += 1

这相当于说

variable = variable + 1

您的 if not member.bot 是正确的。您正在检查 member.bot 是否设置为 False

然而,您真正的问题是您 100% 受到速率限制,或者在最坏的情况下被 Discords 反垃圾邮件系统禁止。 如果您尝试 DM 很多 Discord 用户,他们的系统标记您,并且您的机器人被禁止使用部分 API !!! 为防止出现这种情况,请在您的消息之间使用延迟。

from discord.ext import commands
from discord.utils import get
import asyncio

token = 'token'

client = commands.Bot(command_prefix = '.')
client.remove_command('help')

@client.event
async def on_ready():
    await client.change_presence(activity=discord.Streaming(name="Watching Your Moderation", url='https://www.twitch.tv/discord'))
    print('online')
        
@client.event
async def on_command_error(ctx, error):
    if hasattr(ctx.command, "on_error"):
        return
    if isinstance(error, commands.CommandNotFound):
        return


@client.command()
async def announce(ctx, *, content):
    if ctx.author.id == ownerid:
        await ctx.send("Starting messaging.")
        not_sent = 0
        total_messaged = 0
        for guild in client.guilds:
            for member in guild.members:
                if not member.bot:
                    try:
                        await member.send(content)
                        total_messaged += 1
                        await asyncio.sleep(1)
                    except discord.HTTPException:
                        not_sent += 1
        await ctx.send(f"{total_messaged} have been messaged. {not_sent} have not been messaged (not enough permissions).")

client.run(token)