【问题标题】:Discord Bot Task Not UpdatingDiscord Bot 任务未更新
【发布时间】:2020-07-17 01:59:55
【问题描述】:

我正在尝试在 discord 服务器上创建一组语音通道来显示统计信息,但我的任务由于某种原因没有更新。不知道为什么。如果通道不存在,它将创建通道,但如果它们已经存在,则不会更新它们。

@tasks.loop(seconds = 30)
async def serverstats():
    guild = discord.utils.get(bot.guilds)
    total_channels = len(guild.text_channels) + len(guild.voice_channels)
    list_channels = []
    perms = {
        guild.default_role: discord.PermissionOverwrite(connect = False, view_channel = True)
    }

    for channel in guild.channels:
        list_channels.append(channel.name)

    if 'Server Stats' not in list_channels:
        stat_category = await guild.create_category('Server Stats')
        stat_memb_channel = await guild.create_voice_channel(f'Users: {len(guild.members)} ', overwrites=perms, category = stat_category)
        stat_role_channel = await guild.create_voice_channel(f'Roles: {len(guild.roles)} ', overwrites=perms, category = stat_category)
        stat_voice_channel = await guild.create_voice_channel(f'Channels: {total_channels} ', overwrites=perms, category = stat_category)
    if 'Server Stats' in list_channels:
        await stat_memb_channel.edit(name = f'Users: {len(guild.members)}')
        await stat_role_channel.edit(name = f'Roles: {len(guild.roles)}')
        await stat_voice_channel.edit(name = f'Channels: {total_channels} ')

经过进一步调查,删除底部 for 循环修复了机器人停止任务循环,但没有解决我的问题。

编辑:这是键入解决方案的作者的清理代码。有一些语法错误,但代码的总体思路都是正确的。

@tasks.loop(seconds = 15)
async def serverstats():
    guild = discord.utils.get(bot.guilds)
    stat_memb_channel = None
    stat_role_channel = None
    stat_voice_channel = None
    stat_category = None
    bot_channel = None
    perms = {
        guild.default_role: discord.PermissionOverwrite(connect = False, view_channel = True)
    }

    for channel in guild.channels:
        if channel.name.startswith('Server Stats'):
            stat_category = channel
        if channel.name.startswith('Users: '):
            stat_memb_channel = channel
        if channel.name.startswith('Roles: '):
            stat_role_channel = channel
        if channel.name.startswith('Channels: '):
            stat_voice_channel = channel
        if channel.name.startswith('Bots: '):
            bot_channel = channel
    Bots = 0
    for member in guild.members:
        for role in member.roles:
            if role.name == 'Bot':
                Bots += 1


    if stat_category is None:
        stat_category = await guild.create_category('Server Stats')
    if bot_channel is None:
        bot_channel = await guild.create_voice_channel(f'Bots: {Bots} ', overwrites=perms, category = stat_category)
    else:
        await bot_channel.edit(name = f'Bots: {Bots}')
    if stat_memb_channel is None:
        stat_memb_channel = await guild.create_voice_channel(f'Users: {len(guild.members)} ', overwrites=perms, category = stat_category)
    else:
        await stat_memb_channel.edit(name = f'Users: {len(guild.members)}')
    if stat_role_channel is None:
        stat_role_channel = await guild.create_voice_channel(f'Roles: {len(guild.roles)} ', overwrites=perms, category = stat_category)
    else:
        await stat_role_channel.edit(name = f'Roles: {len(guild.roles)}')
    if stat_voice_channel is None:
        total_channels = len(guild.text_channels) + len(guild.voice_channels) 
        stat_voice_channel = await guild.create_voice_channel(f'Channels: {total_channels} ', overwrites=perms, category = stat_category)
    else:
        total_channels = len(guild.text_channels) + len(guild.voice_channels) 
        await stat_voice_channel.edit(name = f'Channels: {total_channels - 4} ')

【问题讨论】:

  • 如果Server Stats 已经退出,那么stat_memb_channel 和其他通道变量永远不会被定义。您应该在第一个 for 循环中添加一个 else 子句以获取这些频道。

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


【解决方案1】:

这是一种方法:

@tasks.loop(seconds = 30)
async def serverstats():
    guild = discord.utils.get(bot.guilds)
    stat_memb_channel = None
    stat_role_channel = None
    stat_voice_channel = None
    stat_category = None
    perms = {
        guild.default_role: discord.PermissionOverwrite(connect = False, view_channel = True)
    }

    for channel in guild.channels:
        if channel.name.startswith('Server Stats'):
            stat_category = channel
        if channel.name.startswith('Users: '):
            stat_memb_channel = channel
        if channel.name.startswith('Roles: '):
            stat_roles_channel = channel
        if channel.name.startswith('Channels: '):
            stat_voice_channel = channel

    if stat_category is None:
        stat_category = await guild.create_category('Server Stats')
    if stat_memb_channel is None:
        stat_memb_channel = await guild.create_voice_channel(f'Users: {len(guild.members)} ', overwrites=perms, category = stat_category)
    else:
        await stat_memb_channel.edit(name = f'Users: {len(guild.members)}')
    if stat_role_channel is None:
        stat_role_channel = await guild.create_voice_channel(f'Roles: {len(guild.roles)} ', overwrites=perms, category = stat_category)
    else:
        await stat_role_channel.edit(name = f'Roles: {len(guild.roles)}')
    if stat_voice_channel is None:
        total_channels = len(guild.text_channels) + len(guild.voice_channels) + 1
        stat_voice_channel = await guild.create_voice_channel(f'Channels: {total_channels} ', overwrites=perms, category = stat_category)
    else:
        await stat_voice_channel.edit(name = f'Channels: {total_channels} ')

【讨论】:

    猜你喜欢
    • 2021-04-15
    • 2021-11-10
    • 2021-05-17
    • 2020-10-18
    • 2021-03-27
    • 2021-07-19
    • 2021-04-16
    • 2021-10-29
    • 1970-01-01
    相关资源
    最近更新 更多