geas-bot/app/dev_old.py
Vivek Santayana ef6c49b5f8 15 July Build
Implemented YAML
Implemented basic client introspection for guild metadata
Added todo tracker
2021-07-15 09:03:44 +01:00

189 lines
6.0 KiB
Python

import os # OS Locations
from dotenv import load_dotenv # Import OS variables from Dotenv file.
load_dotenv() # Load Dotenv. Delete this for production
import configparser # Config ini parser for Bot config files
import json # Json Library to manage json Data files
import discord # Main Lib
from discord.ext import commands # Commands module
from discord_slash import SlashCommand, SlashContext, cog_ext, utils # Slash Command Library
from discord_slash.utils.manage_commands import create_choice, create_option # Slash Command features
# Locate configuration file
configFile = './data/config.ini'
# Create empty list of current guilds, to be populated Bot load
guild_ids = []
# Config keys
configKeys = ['botrole', 'committeerole', 'modchannel', 'prefix']
# Bot Prefix Function for Regular String Prefix (non-Slash commands)
def getPrefix(client,message):
conf = configparser.ConfigParser()
conf.read(configFile)
if message.guild.id in conf.sections():
if 'prefix' in conf[message.guild.id]:
return conf[message.guild.id]['prefix']
pass
# Define bot client and initialise Slash Command client
client = commands.Bot(command_prefix= '¬', intents=discord.Intents.all()) # Regular client set up, but I did not know you could pass a function as a prefix!
slash = SlashCommand(client, sync_commands=True) # Enable Slash Command Functionality
# Starting Bot Initialisation
@client.event
async def on_guild_join(guild):
pass
# @slash.slash(
# name='configure',
# description='Configuration command to set up the various parameters for the bot on the server.'
# guild_ids=guild_ids,
# options = [
# create_option(
# name='parameter1',
# description='Please select the first parameter you would like to configure.',
# type=3,
# required=True
# )
# ]
# )
# On Ready
@client.event
async def on_ready():
print('Bot Ready')
for guild in client.guilds:
guild_ids.append(guild.id)
channel = discord.utils.get(guild.text_channels,position=1)
print(channel)
print(guild_ids)
conf = configparser.ConfigParser()
conf.read(configFile)
undef = []
if guild.id in conf.sections(): # Check if there are configs for the guild, and check if configs are complete
for i in configKeys:
if i not in conf[guild.id]:
undef.append(i)
if len(undef) == 0: # If none of the key values are undefined, ignore it.
channel = discord.utils.get(guild.text_channels,id=conf[guild.id]['modchannel'])
output = f'`{client.user.display_name}` has already been configured for the guild `{guild.name}`. \n'
output = ''.join([output, f'The `botrole` for the guild `{guild.name}` is {discord.utils.get(guild.roles,id=conf[guild.id]["botrole"]).mention}\n'])
output = ''.join([output, f'The `committeerole` for the guild `{guild.name}` is {discord.utils.get(guild.roles,id=conf[guild.id]["committeerole"]).mention}\n'])
output = ''.join([output, f'The `modchannel` for the guild `{guild.name}` is {channel.mention}\n'])
output = ''.join([output, f'The `prefix` for the guild `{guild.name}` is `{conf[guild.id]["prefix"]}`'])
await channel.send(output)
break
if len(undef) == 0:
undef = configKeys.copy()
output = f'`{client.user.display_name}` has not been configured for the guild `{guild.name}`. Please define:\n'
for u in undef:
output = ''.join([output, f'`{u}`\n'])
output = ''.join([output, f'using the `/configure` command.'])
await channel.send(output)
@slash.slash(
name='hello',
description='Hello World command',
guild_ids=guild_ids,
options = [
create_option(
name='option',
description='choose your word',
required=True,
option_type=3
)
]
)
async def _hello(ctx:SlashContext, option:str):
await ctx.send(option)
@slash.slash(
name='mypfp',
description='Displays profile picture',
guild_ids=guild_ids
)
async def pfp(ctx):
embed = discord.Embed(
title=f'Avatar of {ctx.author.display_name}',
color=discord.Color.teal()
).set_image(url=ctx.author.avatar_url)
await ctx.send(embed=embed)
class Cogs(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command(description='Sends Pong')
async def ping(self, ctx):
await ctx.send('pong')
@cog_ext.cog_slash(name='Ping', description='Sends Pong')
async def ping(self, ctx):
await ctx.send('pong')
@client.command(name='foo')
async def foo(ctx):
f = await utils.manage_commands.get_all_commands(client.user.id,os.getenv('TEST_3_TOKEN'),guild_id=ctx.guild.id)
print(f)
def setup(bot):
client.add_cog(Cogs(client))
###### Configuration Cog
# class Configuration(commands.Cog):
# def __init__(self, client):
# self.client = client
# @cog_ext.cog_slash(
# # base='botrole',
# # subcommand_group='configure',
# name='configure',
# description='Parameter to define the role that is assigned to the dice bots in this guild so they can access in-game text channels.',
# # base_description='Command to configure the various guild parameters.',
# # subcommand_group_description='These are configuration commands to set up the various guild parameters.',
# guild_ids=guild_ids
# # options=[
# # create_option(
# # name='botrole',
# # description='The role that the dice bots are assigned in order to access the text channels.'
# # type=8,
# # required=True
# # )
# # ]
# )
# async def _configure(self, ctx:SlashContext, option):
# await ctx.send(f'The `botrole` for the guild `{ctx.guild.name}` has been set to `{option.mention}`.')
# def setup(client):
# client.add_cog(Configuration(client))
client.run(os.getenv('TEST_3_TOKEN'))
# guilds = [
# 'guild1',
# 'guild2',
# 'guild3'
# ]
# configFile = './config.ini'
# config = configparser.RawConfigParser()
# for g in guilds:
# config.add_section(g)
# config.set(g,'botrole',f'<Bot Role> for {g}')
# config.set(g,'committeerole',f'<Committee Role> for {g}')
# with open(configFile,'w') as c:
# config.write(c)
# parser = configparser.ConfigParser()
# parser.read(configFile)
# print(parser.sections())
# print(parser['guild1']['botrole'])