forked from harkano/maddie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaddie2.py
141 lines (110 loc) · 4.01 KB
/
maddie2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# bot.py
import os
import discord
from discord.ext import commands
import json
import re
import logging
#import owner
from dotenv import load_dotenv
from utils import get_modified_num, get_moves as get_moves_array
from moves import get_moves
from command_handler import plain_command_handler, embed_command_handler
from parse import mad_parse, add_result
logger = logging.getLogger('discord')
logger.setLevel(logging.INFO) #set logging level to INFO, DEBUG if we want the full dump
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='a') #open log file in append mode
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
load_dotenv()
print (os.path.basename(__file__))
if os.path.basename(__file__) == 'maddie2.py':TOKEN = os.getenv('DISCORD_TOKEN_DEV')
else:TOKEN = os.getenv('DISCORD_TOKEN')
print (TOKEN)
def get_prefix(bot, message):
if not message.guild:
return commands.when_mentioned_or("!")(bot, message)
with open("prefixes.json", 'r') as f:
prefixes = json.load(f)
if str(message.guild.id) not in prefixes:
return commands.when_mentioned_or("!")(bot, message)
prefix = prefixes[str(message.guild.id)]
return commands.when_mentioned_or(prefix)(bot,message)
logger.info (TOKEN)
#client = discord.Client()
client = commands.Bot(command_prefix=get_prefix)
async def is_guild_owner(ctx):
return ctx.author.id == ctx.guild.owner.id
print (ctx.author_id, ctx.guild.owner.id)
async def prefix(self, ctx, *, pre):
with open(r"prefixes.json", 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = pre
await ctx.send(f"New prefix is `{pre}`")
with open(r"prefixes.json", 'r') as f:
json.dump(prefixes, f, indent=4)
@client.event
async def on_ready():
logger.info(f'{client.user} has connected to Discord!')
servers = list(client.guilds)
logger.info("Connected on "+str(len(client.guilds))+" servers:")
for x in range(len(servers)):
logger.info(' ' + servers[x-1].name)
#Listen for messages
@client.event
async def on_message(message):
pre = get_prefix
print (pre)
if message.author == client.user:
return
# handle help and all of the playbook interactions
response = plain_command_handler(message)
if response:
await message.channel.send(response)
return
response = embed_command_handler(message)
if response:
await message.channel.send(embed=response)
return
#list moves
move_list = get_moves(message)
if move_list:
await message.channel.send(move_list)
return
#remember generic ! should always be last in the tree
elif message.content.startswith("!"):
log_line = message.guild.name + "|" + message.channel.name + "|" + message.author.name + "|" + message.content
logger.info(log_line)
response = mad_parse(message)
if response: await message.channel.send(embed=response)
await client.process_commands(message)
@client.command()
async def ping(ctx):
await ctx.send("Pong!")
@client.command()
async def test(ctx, arg):
await ctx.send(arg)
@client.command()
async def moves(ctx):
##Load in the existing moves
json_array = get_moves_array()
response = ''
for p in json_array['moves']:
response = response + p['shortName'] + ", "
await ctx.send(response)
#Leaving this out for now as i like the message regex match for being more flexible, but this would be the right way to do it
#@client.command(aliases=moves_list)
#async def move(ctx, *, words):
# print (words, ctx)
# await ctx.send(embed=mad_parse(ctx.c, ctx.author.display_name))
@client.command()
@commands.is_owner()
async def prefix(ctx, *, pre):
with open(r"prefixes.json", 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = pre
await ctx.send(f"New prefix is `{pre}`")
with open(r"prefixes.json", 'w') as f:
json.dump(prefixes, f, indent=4)
#runs the bot!
client.run(TOKEN)