pybot-f/pybot-f.py

97 lines
2.6 KiB
Python

# pybot-f by freakyy85 (freaky@freakyonline.de)
import os
import configparser
import discord
import asyncio
import typing
from discord.ext import commands
import logging
# Logging
logger = logging.getLogger('discord')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='bot.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
# The bots' description
description = '''Testing this bot stuff :)'''
# Intents
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
# Config loading
config = configparser.ConfigParser()
config.read('bot.ini')
bottoken = config['General']['bottoken']
# Setting up commands.Bot
bot = commands.Bot(command_prefix='.', description=description, intents=intents)
@bot.event
async def on_ready():
print(f'pybot-f logged in as {bot.user} (ID: {bot.user.id})')
print('------')
@bot.command()
async def repeat(ctx, times: int, content='repeating...'):
"""Repeats a message multiple times."""
for i in range(times):
await ctx.send(content)
@bot.command()
@commands.is_owner()
async def sync(ctx: commands.Context, guilds: commands.Greedy[discord.Object],
spec: typing.Optional[typing.Literal["~", "*"]] = None) -> None:
if not guilds:
if spec == "~":
fmt = await ctx.bot.tree.sync(guild=ctx.guild)
elif spec == "*":
ctx.bot.tree.copy_global_to(guild=ctx.guild)
fmt = await ctx.bot.tree.sync(guild=ctx.guild)
else:
fmt = await ctx.bot.tree.sync()
await ctx.send(
f"Synced {len(fmt)} commands {'globally' if spec is None else 'to the current guild. :)'}"
)
return
fmt = 0
for guild in guilds:
try:
await ctx.bot.tree.sync(guild=guild)
except discord.HTTPException:
pass
else:
fmt += 1
await ctx.send(f"Synced the tree to {fmt}/{len(guilds)} guilds.")
# loading extensions ...
async def scanext():
with os.scandir('./ext/') as dirs:
for entry in dirs:
if entry.name.endswith('.py'):
try:
print(f"Loading extension: {entry.name}")
await bot.load_extension('ext.' + os.path.splitext(entry.name)[0])
except Exception as error:
print(f'Error loading {entry.name}: {error}')
async def main():
async with bot:
await scanext()
await bot.start(bottoken)
asyncio.run(main())