0

I was trying to develop a bot using discord.py that will be able to go through a specific channel given the channel ID, and count the occurrence of a specific phrase, then return the top 10 users that have said that phrase along with the number of times they've said it.

Example of what I'm thinking:

  • !phrase_7 – returns the top users whose had the most occurrences in the last 7 days
  • !phrase_14 – returns the top users whose had the most occurrences in the last 14 day
  • !phrase_21 – returns the top users whose had the most occurrences in the last 21 days
  • !phrase_30 – returns the top users whose had the most occurrences in the last 30 days

This is what I have so far:

import os
import discord
import datetime as dt
from discord.ext import commands
from decouple import event
from discord import channel
from dotenv import load_dotenv

""" client.py serves as the connection to discord"""
# Declaration of client variables
PRIMARY_GUILD = " "
PRIMARY_GUILD_KEY = 823567841297327808
WAGON_STEAL_CHANNEL_KEY = 823567841297327742
client = discord.Client()
bot_user = client.user
bot = commands.Bot(command_prefix='!')


# Declaration of Bot Security - bot token ID is stored in a separate .env file to ensure encapsulation of data
load_dotenv()
TOKEN = os.getenv('TOKEN_ID')  # protected in .env file


# Declaration of Event Logic
@client.event
async def on_ready():
    """ Confirms the bot has successfully connected to the server we targeted """
    for guild in client.guilds:
        if guild.name == PRIMARY_GUILD:
            print("Locked In ")  # we are where we want to be
        else:
            print("Name's didn't match ")

    print(f'{client.user} has successfully connected to {guild.name} ')


@bot.command()
async def phrase(ctx, days: int = None):
    if days:
        after_date = dt.datetime.utcnow()-dt.timedelta(days=days)

        messages = await ctx.channel.history(limit=10, oldest_first=True, after=after_date).flatten()
        print(messages)
    else:
        await ctx.send("please enter the number of days wanted")


client.run(TOKEN)

The code I have above is connected to discord, but when running, !phrase 4, it does not return anything.

  • Is the specific phrase always the same? if yes, can you provide an example phrase, if not then how would you get the phrase from the user, as a parameter? – Wasi Master Jul 26 '21 at 04:42
  • Yes! I apologize for not clarifying. The phrase would always be the same, so it could be considered a constant. **example** It would find every user in a specific channel that said 'keyboard'. Then count the occurrences of 'keyboard' through a chancels DMs and return the amount of time a specific user said that. Tyler - 20 times Jackson - 12 times ... Eliot - 4 times –  Jul 26 '21 at 05:07

1 Answers1

0

You make a command with an int argument called days then take the timedelta from the time the command invoked.

  • !phrase 4 prints all the message for the last 4 days.

  • !phrase 14 prints all the message for the last 14 days.

# import datatime as dt

@bot.command()
async def phrase(ctx, days: int = None):
    if days:
        after_date = dt.datetime.utcnow()-dt.timedelta(days=days)
        # limit can be changed to None but that this would make it a slow operation.
        messages = await ctx.channel.history(limit=10, oldest_first=True, after=after_date).flatten()
        print(messages)
    else:
        await ctx.send("please enter the number of days wanted")

TextChannel.history

Returns an AsyncIterator that enables receiving the destination’s message history. You must have read_message_history permissions to use this.

Abdulaziz
  • 3,363
  • 1
  • 7
  • 24