Code4GovTech / discord-bot

Official Bot for C4GT - please verify the https and URL before registering.
https://bot.c4gt.samagra.io
1 stars 12 forks source link

Create a periodic task for collecting messages from discord server. #6

Closed KDwevedi closed 1 year ago

KDwevedi commented 1 year ago

Description

The command add_messages collects messages from the C4GT Discord server and saves data in a database. There is a need to have this task be automated so that it repeats at regular intervals (or at specific times) in the background while the bot is active

An illustration of a possible implementation for reference:

@tasks.loop(seconds=60)  # Runs every 60 seconds
async def my_task():
    guild_id = 1234567890  # Replace with the actual guild ID
    guild = bot.get_guild(guild_id)
    channel_id = 1234567890  # Replace with the actual channel ID
    channel = guild.get_channel(channel_id)

    start_command = bot.get_command('start_task')
    stop_command = bot.get_command('stop_task')

    ctx = await bot.get_context(channel)
    await start_command.invoke(ctx)

    await asyncio.sleep(10)  # Wait for 10 seconds

    await stop_command.invoke(ctx)

@bot.command()
async def start_task(ctx):
    my_task.start()
    await ctx.send('Task started!')

@bot.command()
async def stop_task(ctx):
    my_task.stop()
    await ctx.send('Task stopped!')

bot.run('YOUR_BOT_TOKEN')

Goals:

Product Name:

C4GT

Project

Official Code4GovTech Discord Bot

Organization Name:

Samagra

Tech Skills Needed:

Python, Quart

Mentor(s)

@KDwevedi

Complexity

Low

Category

Feature

Sub Category

Refactoring, Beginner Friendly

dixitdeeksha commented 1 year ago

Hey @KDwevedi ,

Can I work on this issue? I'm available and interested in working on this issue

Please let me know if I can proceed with this. Thank you!

starks-and-wolves commented 1 year ago

Hi @KDwevedi, I am interested in working on this issue. Can you please assist me with the detailed requirements?

rushi-02 commented 1 year ago

Hi there! My name is Rushikesh Chavan .I believe I can contribute and would like to work on this issue. Can someone guide me on how to get started?

KDwevedi commented 1 year ago

Hey @rushi-02 @dixitdeeksha @starks-and-wolves The requirements here are straightforward. We need this discord bot to have a task that goes through the message history of the discord guild/server it is live on and add it to a database.

Take a look at add_messages which is a command that does the same thing. The difference between a command and a task is that tasks are set up to run automatically at specific times or after specific durations.

For each new run of the task, your function should be saving previous times when this task has run as a date-time stamp (probably channel-wise), so that you don't need to iterate through the entire history every time.

KDwevedi commented 1 year ago

Assume that you have an env variable 'SERVER_ID' that gives you the id of the discord guild/server and add a config.json so that the loop time interval can be configured for this task.

KDwevedi commented 1 year ago

If you're going to be contributing, please take a fork from the development branch of the repository to raise a PR.

starks-and-wolves commented 1 year ago

Hey @KDwevedi, thanks for the detailed description. I ran the Discord data scraper on a test server and got an idea of how Discord scraping works and how data is getting populated in the DB. I will begin implementing the requirements now. Thanks again

starks-and-wolves commented 1 year ago

Hey @KDwevedi, I have raised my first open-source PR. I am not sure if I have followed the correct procedure. It will be great if you can review the PR and give your feedback on it.

c4gt-community-support[bot] commented 1 year ago

Hi! Important Details - These following details are helpful for contributors to effectively identify and contribute to tickets.

Please update the ticket