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.