如何使一个机器人使用discord.py读取一条文本通道中已写入一条消息的次数?

时间:2019-09-26 22:39:13

标签: python search bots discord.py history

这是我的代码: (我使用python 3.7)


    import discord
    import random
    import json
    import asyncio
    from importlib import import_module
    from importlib import reload


    class MyClient(discord.Client):
        async def on_ready(self):
            print('Logged in as')
            print(self.user.name)
            print(self.user.id)
            print('------')
            activity = discord.Activity(name='for someone to hug!', type=discord.ActivityType.watching)
            await client.change_presence(activity=activity)

        async def on_message(self, message):
            # we do not want the bot to reply to itself
            if message.author.id == self.user.id:
                return

            with open("Bobby\code\Bobby\json\on_message.json") as json_file:
                data = json.load(json_file)
                for key in data["command"]: 
                    with open("Bobby\code\Bobby\json\settings.json") as json_file2:
                        data2 = json.load(json_file2)
                        cmd = data2["settings"]["prefix"]+key  
                    if message.content.startswith(cmd):
                        counter = 0
                        async for msg in client.logs_from(message.channel, limit=500):
                            await asyncio.sleep(3)
                            if msg.content.lower() == message.content.lower():
                                counter = counter + 1
                        result = "" + counter
                        await message.channel.send(result)   
                else:
                    for key in data["message"]: 
                        if key in message.content.lower():
                            counter = 0
                            for key2 in data["message"][""+key+""]:
                                if key2 == "channel":
                                    counter = counter + 1
                                    channel = client.get_channel(data["message"][""+key+""]["channel"])
                                    await channel.send(random.choice(data["message"][""+key+""]["messages"]))
                            if counter == 0:
                                await message.channel.send(random.choice(data["message"][""+key+""]["messages"]))



    client = MyClient()
    client.run('my token')

我尝试了所有项目,并且知道除了这部分以外,其他所有功能都可以使用

   async for msg in client.logs_from(message.channel, limit=500):
       await asyncio.sleep(3)
       if msg.content.lower() == message.content.lower():
           counter = counter + 1
   result = "" + counter

我不了解如何使用 logs_from ,我也尝试使用 message.channel.history ,如下所示:

   counter = 0
   async for m in message.channel.history(limit=500):
       await asyncio.sleep(3)
       if message.content.lower() == m.content.lower():
           counter = counter + 1
   result = "" + counter

我又如何在全部中搜索文本通道的历史记录?

我想我在使用 await async 方面有些失败,有人建议我解决此问题吗?我是否应该使用其他方法来实现这一目标?

0 个答案:

没有答案