Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add events cog and move code from main #134

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,34 @@ def main():
intents = discord.Intents.default()
intents.guilds = True
intents.message_content = True

activity = discord.Activity(
type=discord.ActivityType.watching, name="de DDS server"
)

bot = commands.Bot(
command_prefix=commands.when_mentioned_or(config.PREFIX),
intents=intents,
activity=activity,
owner_id=config.OWNER_ID,
)
loaded: list = []

@bot.event
async def on_ready():
print("--- Bot is online and ready to interact! ---")
print(f'{bot.user.name} has connected to Discord (ID: {bot.user.id})')

@bot.event
async def on_message(message: discord.Message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
if message.type == discord.MessageType.default:
print(f'{username} in #{message.channel}: {user_message}')

if message.author == bot.user:
return

# msg = message.content

# if any(word in msg for word in sad_words):
# await message.reply(random.choice(happy_response))

# Leave this here, otherwise commands wil stop running
await bot.process_commands(message)
# Load all modules (cogs)
for module in os.listdir('modules'):
try:
bot.load_extension(f'modules.{module}')
loaded.append(module)
except Exception as error:
print(f'Failed to load module {module}: {error}')

# Load all modules
for folder in os.listdir('modules'):
bot.load_extension(f'modules.{folder}')
# Print all loaded modules
loaded = ', '.join(loaded)
print(f'Successfully loaded modules (cogs): {loaded}')

# Run the bot
bot.run(config.BOT_TOKEN)

# Run the bot
# Run main() to start the bot
if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion modules/button_roles/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Cog for giving and removing roles based on button presses."""
from discord.ext import commands
from .role_view import RoleView


class ButtonRoles(commands.Cog, name="Button Roles"):
"""Give and remove roles based on button presses."""
"""ButtonRoles class for handling button roles."""

def __init__(self, bot: commands.Bot):
self.bot = bot
Expand Down
1 change: 1 addition & 0 deletions modules/button_roles/role_view.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""UI View for the role buttons."""
from utils.utils import custom_id
import discord
import config
Expand Down
36 changes: 36 additions & 0 deletions modules/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Cog for handling Discord events."""
import discord

from discord.ext import commands


class Events(commands.Cog, name="Events"):
"""Events class for handling Discord events."""
def __init__(self, bot: commands.Bot):
self.bot = bot

@commands.Cog.listener()
async def on_ready(self):
print("--- Bot is online and ready to interact! ---")
print(f'{self.bot.user.name} has connected to Discord (ID: {self.bot.user.id})')

@commands.Cog.listener()
async def on_message(self, message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
if message.type == discord.MessageType.default:
print(f'{username} in #{message.channel}: {user_message}')

if message.author == self.bot.user:
return

# msg = message.content

# if any(word in msg for word in sad_words):
# await message.reply(random.choice(happy_response))

# Leave this here, otherwise commands wil stop running
await self.bot.process_commands(message)

def setup(bot: commands.Bot):
bot.add_cog(Events(bot))