pybot-f/pybot-f.py

96 lines
2.4 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
2022-05-27 19:35:46 +02:00
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():
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:
2022-05-26 21:50:10 +02:00
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(
2022-05-27 19:35:46 +02:00
f"Synced {len(fmt)} commands {'globally' if spec is None else 'to the current guild. :)'}"
2022-05-26 21:50:10 +02:00
)
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.")
2022-09-03 08:51:28 +02:00
# loading extensions ...
async def scanext():
print("Loading extensions:")
2022-11-07 22:31:47 +01:00
for filename in os.listdir("./ext"):
if filename.endswith(".py"):
await bot.load_extension(f"ext.{filename[:-3]}")
print(f'Loaded: {filename}')
print('\n')
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())