forked from nxg9997/ScopeBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
74 lines (56 loc) · 2 KB
/
bot.py
File metadata and controls
74 lines (56 loc) · 2 KB
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
# Perm Int: 3406912
# Import Libraries / Modules
import discord
import random
import os
# Only import Config.py if the bot is being run locally
# 'HOSTED' is a environment variable set on Heroku that equals the bot token
token = ''
if os.environ.get('HOSTED') == None:
import config
token = config.token
else:
token = str(os.environ.get('HOSTED'))
print('Running on Heroku, token = ' + token)
import data
# create a Discord client instance
client = discord.Client()
# Helper functions
# Retrieves a quote, and will @ the user if supported by the quote
def GetQuote(usr):
quote = data.scopebook[random.randint(0,len(data.scopebook)-1)]
quote = quote.replace('$','<@' + str(usr) + '>')
return quote
# Check if a message contains a keyword
def ContainsKeyword(msg):
if any(map(msg.content.lower().__contains__,data.keywords)):
return True
return False
# Discord Events
# When the bot is successfully logged in
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
# When a message is sent to a channel visible to the bot
@client.event
async def on_message(msg):
# enforces cooldown on sending scope messages
if not str(msg.channel.id) in data.cooldowns:
data.cooldowns[str(msg.channel.id)] = 0
elif data.cooldowns[str(msg.channel.id)] > 0:
data.cooldowns[str(msg.channel.id)] -= 1
# ignore messages sent by the bot
if msg.author == client.user:
return
# 'hello' ping
if msg.content.startswith('$hello'):
await msg.channel.send('Hello!')
# send a scope reminder (by using a command, random chance, or by containing a keyword)
if msg.content.startswith('!scope'):
await msg.channel.send(GetQuote(msg.author.id))
elif (random.randint(0,100) <= 5 or ContainsKeyword(msg)) and data.cooldowns[str(msg.channel.id)] == 0:
data.cooldowns[str(msg.channel.id)] = 10
await msg.channel.send(GetQuote(msg.author.id))
# run the bot
print('Bot online')
client.run(token)