pybot-f/pybot-f.py

84 lines
2.2 KiB
Python
Raw Normal View History

# pybot-f by freakyy85 (freaky@freakyonline.de)
2022-05-26 16:26:08 +02:00
import os
import configparser
import discord
import asyncio
2022-05-26 21:50:10 +02:00
import typing
from discord.ext import commands
description = '''Testing this bot stuff :)'''
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
# Config loading
config = configparser.ConfigParser()
config.read('bot.ini')
bottoken = config['General']['bottoken']
2022-05-26 23:41:17 +02:00
2022-05-26 21:50:10 +02:00
bot = commands.Bot(command_prefix='.', description=description, intents=intents)
@bot.event
async def on_ready():
2022-05-26 23:41:17 +02:00
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()
2022-05-26 21:50:10 +02:00
@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.")
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}')
2022-05-26 16:26:08 +02:00
async def main():
async with bot:
await scanext()
await bot.start(bottoken)
2022-05-26 21:50:10 +02:00
asyncio.run(main())