-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
199 lines (154 loc) · 6.21 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from __future__ import print_function
import os
# library for discord.py API
import discord
import asyncio
from discord.ext import commands, tasks
# library for file I/O
from dotenv import load_dotenv
import json
# library for Google Calendar API
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
# library to parse natural language into datetime
import parsedatetime
# library for naive and aware datetime
# resource: https://vinta.ws/code/timezone-in-python-offset-naive-and-offset-aware-datetimes.html
# resource: https://stackoverflow.com/questions/13866926/is-there-a-list-of-pytz-timezones
import tzlocal
# Cog
import greet
import alert_cog
import reminder_cog
# Load discord token from config file
with open('./config.json', 'r') as f:
config_dict = json.load(f)
TOKEN = config_dict['token']
# create an instance of client (connect it to Discord WebSocket API)
# client = discord.Client()
bot = commands.Bot(command_prefix='.')
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar.events']
creds = None
service = None
DEFAULT_COLOR = discord.Color.blue()
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
global creds, service
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
with open('token.json', 'r') as stream:
creds_json = json.load(stream)
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
creds.token = creds_json['token']
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('calendar', 'v3', credentials=creds)
# bot.add_cog(alert_cog.AlertCog(bot, service))
# TODO This activate every time a bot use outside module. Figure a new way to greet user
# Send bot login message as chat in a first text channel
# for guild in bot.guilds: # A bot can be deployed in multiple server(guild), so iterate over every server
# await guild.text_channels[0].send('I\'m here mortal')
# channels = client.get_all_channels()
# for channel in channels.text_channels:
# await channel.send('You summon me?')
@bot.event
async def on_message(message):
# check if the message send is not from the bot
if message.author == bot.user:
return
print(message.content)
if message.content.startswith('hello'):
channel = message.channel
await channel.send('Hello!')
await channel.send('How are you?')
def check(m):
return m.content.startswith('good') and m.channel == channel
msg = await bot.wait_for('message', check=check)
await msg.add_reaction('\N{THUMBS UP SIGN}')
await channel.send('That\'s awesome {.author}!'.format(msg))
if message.content.startswith('thumb'):
channel = message.channel
await channel.send('gimme that 👍 reaction, mate')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '👍'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('👎')
else:
await channel.send('thank you 👍')
if message.content.startswith('em'):
channel = message.channel
embed = discord.Embed(title='Sample Title lol',
description='This is a long description\n ipsum lol mao',
color=discord.Color.blue())
embed.insert_field_at(0, name='name1', value='bruh this is hmmmmmmmmmmmmmm', inline=False)
embed.insert_field_at(1, name='name2', value='bruh this is hmmmmmmmmmmmmm2', inline=False)
embed.insert_field_at(2, name='name3', value='>>> inside', inline=False)
embed.insert_field_at(3, name="name4", value='> inside', inline=False)
val = ""
for i in range(3):
val = (embed.fields[2].value + "\nnext{}".format(i))
embed.set_field_at(2, name='name3', value=val, inline=False)
embed.set_footer(text='Footerlrlrlr')
await channel.send(embed=embed)
if "smart" in message.content and "not" not in message.content:
await message.add_reaction('\N{THUMBS UP SIGN}')
await message.channel.send('Thank you')
await bot.process_commands(message)
# @bot.event
# async def on_command_error(ctx, error):
# # await ctx.send(error)
# print(error)
# Testing converter for function
# def to_upper(argument):
# return argument.upper()
#
#
# @bot.command()
# async def up(ctx, *, content: to_upper):
# await ctx.send(content)
# Testing Converter concept for user defined class
# import random
#
#
# class Slapper(commands.Converter):
# async def convert(self, ctx, argument):
# to_slap = random.choice(ctx.guild.members)
# return '{.author.display_name} slapped {.display_name} because *{}*'.format(ctx, to_slap, argument)
#
#
# @bot.command()
# async def slap(ctx, *, reason: Slapper):
# await ctx.send(reason)
@bot.command(name='test')
async def test(ctx, *args):
await ctx.send('Did you {} say {} words: {}'.format(ctx.author, len(args), ', '.join(args)))
await ctx.send('And that is in {} channel'.format(ctx.message.channel))
@bot.command()
async def here(ctx):
await ctx.send('Still here', tts=True)
# TODO Delete calendar event command
# Add Cog
bot.add_cog(greet.Greetings(bot))
bot.add_cog(reminder_cog.ReminderCog(bot))
# Run the bot
bot.run(TOKEN)