简体   繁体   中英

How to make discord bot type in chat to a member who launched the game

            for member in ctx.guild.members: 
                for act in member.activities: 
                    if act.name == 'Game1':
                        await ctx.send(member.mention + 'text')
                    if act.name == 'Game2':
                        await ctx.send(member.mention + 'text')
                    if act.name == 'Game3':
                        await ctx.send(member.mention + 'text'))

When I launch this code, bot starts spaming to the users. What should I do to make it mention user only once and then continue the cycle?

Add a return statement after your await statements. Also, remove the extra ) at the end of the code.

for member in ctx.guild.members: 
    for act in member.activities: 
        if act.name == 'Game1':
            await ctx.send(member.mention + 'text')
            return
        if act.name == 'Game2':
            await ctx.send(member.mention + 'text')
            return
        if act.name == 'Game3':
            await ctx.send(member.mention + 'text'))
            return

I solved my problem:

self.mentioned_users = []
for member in ctx.guild.members:
            for act in member.activities:
                if act.type == discord.ActivityType.playing:
                    if act.name == 'game' and member not in self.mentioned_members:
                        await ctx.send('{0.mention} text'.format(member))
                        self.mentioned_members.append(member)
                    elif act.name == 'game1' and member not in self.mentioned_members:
                        await ctx.send('{0.mention} text'.format(member))
                        self.mentioned_members.append(member)
                    elif act.name == 'game3' and member not in self.mentioned_members:
                        await ctx.send('{0.mention} text'.format(member))
                        self.mentioned_members.append(member)
                elif member in self.mentioned_members:
                    self.mentioned_members.remove(member)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM