-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (82 loc) · 2.82 KB
/
main.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
# Import required modules
import discord
from discord.ext import commands
import os
import random
import csv
# Define some global variables
admin = "<@821883083313381436>"
csv_file = 'data.csv'
command_prefix = '.'
description = '''
Howdy, my name is **Motiovotionabot**
*Quote bot made and hosted by Evan Kim v0.1*
[Github Repo](https://github.com/Evanston09/motiovotionabot/)
'''
# Functions to interact with csv file
def store(csv_file_path, body, author):
with open(csv_file_path, 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([body, author])
# Check if non-existent or empty
def is_file_empty(csv_file_path):
if not os.path.exists(csv_file_path):
return True
with open(csv_file_path, "r") as csvfile:
csv_reader = csv.reader(csvfile)
return not any(csv_reader)
# Pick random quote out of CSV
def pick_random(csv_file_path):
with open(csv_file_path, "r") as csvfile:
csv_reader = csv.reader(csvfile)
data = [line for line in csv_reader]
return random.choice(data)
# Retrieves token DO NOT SHARE
try:
with open("token.txt", "r") as token_file:
token = token_file.read().strip()
except FileNotFoundError:
print("Add token in token.txt!")
quit()
# Sets up intents and the bot
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix=command_prefix, intents=intents, description=description)
# Define description
@bot.command()
async def whoareyou(ctx):
await ctx.send(bot.description)
# Submission
@bot.command()
async def submit(ctx, body: str = None):
# Get quote information
author = ctx.message.author.mention
# Check if empty
if not body:
await ctx.send("You silly goose you didn't submit a quote:man_facepalming:")
return
# Store quote
store(csv_file, body, author)
await ctx.send("Quote submitted successfully!!!")
# Define quote output
@bot.command()
async def quote(ctx):
# Check if empty or non-existent
if is_file_empty(csv_file):
await ctx.send(
"# No quotes have been submitted yet :frowning:\n__You can do that with **.submit [creative quote here]**__")
# Find random quote and create an embed
else:
body, author = pick_random(csv_file)
embed = discord.Embed(title="An inspirational quote for you :fire:",
description=body,
color=discord.Color.from_rgb(42, 82, 190)
)
embed.add_field(name="Submitted by:", value=author, inline=True)
embed.add_field(name="Want to submit your own?", value=".submit [your cool quote]", inline=True)
await ctx.send(embed=embed)
# Run bot KEEP TOKEN A SECRET
try:
bot.run(token)
except discord.errors.LoginFailure:
print("Invalid token!")