-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
122 lines (89 loc) · 3.11 KB
/
bot.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
# BasicBot
# Code lifted from StockImage bot by meed223 with other minor contributors
import logging
import discord
from regex import regex
import Commands
logging.basicConfig(filename="log.txt", level=logging.DEBUG, filemode="w")
# Global Variables
client = discord.Client()
TOKEN = ""
BOT_PREFIX = ""
class Switcher(object):
""" This switcher is based on the names of commands """
def indirect(self, i, message, args):
method = getattr(self, i, lambda: "invalid")
return method(message, args)
def help(self, message, args):
return Commands.bot_help(message)
def status(self, message, args):
return message.channel.send("Bot is Online.")
def roll(self, message, args):
return Commands.roll_dice(message, args)
def flip(self, message, args):
return Commands.flip_coin(message)
def teams(self, message, args):
return Commands.team_gen(message, args)
def ip(self, message, args):
return Commands.get_ip(message)
def announce(self, message, args):
return Commands.announce(message, args)
def setup():
""" Get token & prefix from file and assigns to variables """
file = open("token.txt", "r")
global TOKEN
TOKEN = file.readline().replace("\n", "")
global BOT_PREFIX
BOT_PREFIX = file.readline().replace("\n", "")
file.close()
logging.info(f"Bot token '{TOKEN}' and prefix '{BOT_PREFIX}' are set")
# ---[ Bot Event Code ]---
@client.event
async def on_ready():
""" Set Discord Status """
logging.info("Bot is Ready")
print("Bot is Ready")
await client.change_presence(activity=discord.Activity(
type=discord.ActivityType.listening,
name="commands"))
@client.event
async def on_member_join(member):
""" New member joined server """
print("member joined")
@client.event
async def on_guild_join(guild):
""" Joined a new server """
print("bot joined")
@client.event
async def on_message(message):
""" This is run when a message is received on any channel """
author = message.author
o_args = message.content.split(' ')
if author != client.user and BOT_PREFIX in message.content:
o_args[0] = o_args[0].replace(BOT_PREFIX, "")
args = []
for arg in o_args:
if regex.search("[A-Z]+|[a-z]+|\d+", arg):
args.append(arg)
command = args[0].lower()
del args[0]
switch = Switcher()
await switch.indirect(command, message, args)
def is_authorized(message):
""" Checks user privileges """
authorized = False
for member in message.guild.members:
if member.id == message.author.id:
# Check this ID specifically
for r in member.roles:
if r.permissions.manage_guild:
authorized = True
break
return authorized
if __name__ == "__main__":
try:
setup()
client.run(TOKEN)
except FileNotFoundError:
logging.error("File was not found, "
"are you sure that 'token.txt' exists?")