From c6b54bda37ed5723d6595a2213ca13b0ef2fe47b Mon Sep 17 00:00:00 2001 From: Stefano Assenzo Date: Sun, 29 Jan 2023 14:27:47 +0000 Subject: [PATCH] I try to make the Discord bot with little success, at the moment I focus on something else --- src/bot/discord/commands/moderation/ban.py | 14 ++++ .../discord/commands/moderation/clear_msg.py | 11 +++ src/bot/discord/commands/moderation/kick.py | 11 +++ src/bot/discord/discord_bot.py | 74 ++++++++++++------- src/bot/whatsapp/whatsapp_bot.py | 2 + 5 files changed, 84 insertions(+), 28 deletions(-) create mode 100644 src/bot/discord/commands/moderation/ban.py create mode 100644 src/bot/discord/commands/moderation/clear_msg.py create mode 100644 src/bot/discord/commands/moderation/kick.py diff --git a/src/bot/discord/commands/moderation/ban.py b/src/bot/discord/commands/moderation/ban.py new file mode 100644 index 0000000..7183a67 --- /dev/null +++ b/src/bot/discord/commands/moderation/ban.py @@ -0,0 +1,14 @@ +from discord import Option +import discord + +bot = discord.Bot() + + +@bot.slash_command() +@discord.default_permissions(ban_members = True, administrator = True) +async def ban_user(bot, ctx, member: Option(discord.Member, description="Select a member", required=True), reason: Option(str, description="Reason", required=True)): + try: + await member.ban(reason=reason) + await ctx.respond(f'{member.mention} has been banned!') + except: + await ctx.respond(f'{member.mention} has not been banned!') \ No newline at end of file diff --git a/src/bot/discord/commands/moderation/clear_msg.py b/src/bot/discord/commands/moderation/clear_msg.py new file mode 100644 index 0000000..b9d98ec --- /dev/null +++ b/src/bot/discord/commands/moderation/clear_msg.py @@ -0,0 +1,11 @@ +import discord +from discord.ext import commands + +bot = discord.Bot() + +@bot.slash_command() +@commands.has_permissions(manage_messages=True) +@commands.cooldown(1, 5, commands.BucketType.user) +async def clear_messages(ctx, amount : int): + await ctx.channel.purge(limit=amount+1) + await ctx.respond('Messages have been cleared!') \ No newline at end of file diff --git a/src/bot/discord/commands/moderation/kick.py b/src/bot/discord/commands/moderation/kick.py new file mode 100644 index 0000000..9dda8de --- /dev/null +++ b/src/bot/discord/commands/moderation/kick.py @@ -0,0 +1,11 @@ +import discord +bot = discord.Bot() + +@bot.slash_command() +@discord.default_permissions(kick_members = True, administrator = True) +async def kick_user(bot, ctx, member : discord.Member, *, reason=None): + try: + await member.kick(reason=reason) + await ctx.respond(f'{member.mention} has been kicked!') + except: + await ctx.respond(f'{member.mention} has not been kicked!') \ No newline at end of file diff --git a/src/bot/discord/discord_bot.py b/src/bot/discord/discord_bot.py index 9918190..0380cee 100644 --- a/src/bot/discord/discord_bot.py +++ b/src/bot/discord/discord_bot.py @@ -1,54 +1,56 @@ -from dotenv import load_dotenv -from bson.objectid import ObjectId - -from selenium.webdriver.firefox.options import Options -from selenium.webdriver.common.by import By -from selenium import webdriver - -from discord.ext import tasks, commands -from discord.commands.context import ApplicationContext -from discord.commands import Option -import discord -import pymongo -import urllib.parse -from selenium import webdriver -import datetime -import os -import time +from commands.moderation import ban, kick, clear_msg +from discord.commands.context import ApplicationContext # Discord +from selenium.webdriver.firefox.options import Options # Selenium +from discord.commands import Option # Discord +from discord.ext import commands +from bson.objectid import ObjectId # MongoDB +from selenium import webdriver # Selenium +from dotenv import load_dotenv +from selenium import webdriver # Selenium +from discord.ext import tasks # Discord +import urllib.parse # MongoDB +import datetime # Date +import discord # Discord +import pymongo # MongoDB +import time # Time +import os # OS +# .env load_dotenv() # Load .env file DISCORD_TOKEN = os.getenv('DISCORD_TOKEN') # Discord token GENERAL_ID = os.getenv('GENERAL_ID') # Channel ID PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') # Password for MongoDB URL_MONGODB = os.getenv('URL_MONGODB') # URL for MongoDB + +# MongoDB mongo_url = "mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + URL_MONGODB # URL for MongoDB (with password) client = pymongo.MongoClient(mongo_url) # Connect to MongoDB database = client["website-class"] # Database name -# Collection school time table current -collection = database["school-time-table"] +collection = database["school-time-table"] # Collection school time table current #Date -current_time = datetime.datetime.now() -day = str(current_time.day) -month = str(current_time.month) -year = str(current_time.year) -hour = str(current_time.hour) -minute = str(current_time.minute) -long_date = day + "-" + month + "-" + year + " " + hour + ":" + minute +current_time = datetime.datetime.now() # Current time +day = str(current_time.day) # Day +month = str(current_time.month) # Month +year = str(current_time.year) # Year +hour = str(current_time.hour) # Hour +minute = str(current_time.minute) # Minute +long_date = day + "-" + month + "-" + year + " " + hour + ":" + minute # Long date with day, month, year, hour and minute bot = discord.Bot() @bot.event async def on_ready(): print('We have logged in as {0.user}'.format(bot)) - bot.loop.create_task(orario()) + #bot.loop.create_task(orario()) # Search on MongoDB the subject and send a message on Discord if the subject is found @tasks.loop(seconds=1) async def orario(): documents = collection.find() send_screenshot = 0 -# Iterate through the documents + + # Iterate through the documents for document in documents: for day in document['School Subject']: for i, subject in enumerate(document['School Subject'][day]): @@ -117,4 +119,20 @@ async def change_school_time( {"$set": {f"School Subject.{day}.{int(hour_school)}.Subject": text}} ) +@bot.slash_command() +@discord.default_permissions(ban_members = True, administrator = True) +async def ban(ctx, member: Option(discord.Member, description="Select a member", required=True), reason: Option(str, description="Reason", required=True)): + ban.ban_user(bot, ctx, member, reason) + +@bot.slash_command() +@discord.default_permissions(kick_members = True, administrator = True) +async def kick(ctx, member: Option(discord.Member, description="Select a member", required=True), reason: Option(str, description="Reason", required=True)): + kick.kick_user(bot, ctx, member, reason) + +@bot.slash_command(name= 'clear', description= 'Clears messages from a channel') +@commands.has_permissions(manage_messages=True, administrator=True) +@commands.cooldown(1, 5, commands.BucketType.user) +async def clear(ctx, messages: Option(int, description="Amount of messages to delete", required=True)): + clear_msg.clear_messages(ctx, amount = messages) + bot.run(DISCORD_TOKEN) \ No newline at end of file diff --git a/src/bot/whatsapp/whatsapp_bot.py b/src/bot/whatsapp/whatsapp_bot.py index 8601937..dceb4fa 100644 --- a/src/bot/whatsapp/whatsapp_bot.py +++ b/src/bot/whatsapp/whatsapp_bot.py @@ -1,6 +1,8 @@ +from dotenv import load_dotenv from twilio.rest import Client import os +load_dotenv() SID = os.getenv('SID') AUTH_TOKEN = os.getenv('AUTHTOKEN') PHONE_NUMBER_BOT = os.getenv('PHONE_NUMBER_BOT')