This repository was archived by the owner on Nov 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
173 lines (128 loc) · 5.12 KB
/
main.py
File metadata and controls
173 lines (128 loc) · 5.12 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
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
import discord, praw
import os, random, threading
from discord.ext import commands
from replit import db
from datetime import datetime
from keep_alive import keep_alive
TOKEN = os.getenv("TOKEN")
bot = commands.Bot(command_prefix="def ", help_command=None)
reddit = praw.Reddit()
subreddit = reddit.subreddit("ProgrammerHumor")
trigger_words = ["dank", "dankness"]
spooky_triggers = ["does", "how", "look", "does this", "how is"]
dank_texts = [
"Don't wanna!",
"Busy coding. You should be too!",
"Uh... nah!",
"Nah!",
"Go to `/dev/null!`",
"WHO SUMMONED ME?",
"Ahh, you think dankness is your ally? You merely adopted the dank. I was born in it. Molded by it.",
]
hello_aliases = ["helloThere", "nuqneH", "aiya", "kaltxi", "bello"]
# RECURSION_THRESHOLD = 1 # Will crash the bot
SPOOKINESS_THRESHOLD = 0.1
DANKNESS_THRESHOLD = 0.2
MEMES_CACHED = False
DEFAULT_MEME = "https://othersideoz.ca/blog_images/eoi.jpg"
# To clear MEMES_CACHED
def resetCache():
global MEMES_CACHED
# This function runs periodically every 1 second
threading.Timer(1, resetCache).start()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
if current_time == '00:22:00': # GMT == 0330 IST
db.clear()
MEMES_CACHED = False
print('MEMES_CACHED cleared!')
def cache_memes():
global MEMES_CACHED
if "memes" not in db.keys():
db["memes"] = {
"urls": [DEFAULT_MEME,],
"accessed": [False,],
}
MEMES = db["memes"]
posts = subreddit.hot(limit=100)
exts = ("jpg", "jpeg", "png")
for submission in posts:
url = str(submission.url)
if url.endswith(exts):
MEMES["urls"].append(url)
if len(MEMES["urls"]) >= 2:
db["memes"] = MEMES
MEMES_CACHED = True
def get_meme():
global MEMES_CACHED
if not MEMES_CACHED:
cache_memes()
MEMES = db["memes"]
urls = MEMES["urls"]
if urls:
return random.sample(urls, 1)[0]
return urls[0]
@bot.event
async def on_ready():
print(f"{bot.user.name} is ready to spread dankness now.")
@bot.command(name="help", help="Shows help message and list of commands")
async def help(ctx):
file = discord.File('./master.png')
embed = discord.Embed(
title="List of commands for DankCoder",
description="This is DankCoder - a bot, born to spread dankness in this discord server.\n\n**Bot Pefix**: `def`\n\n**List of Commands:**",
colour=discord.Colour.blurple(),
)
# embed.set_thumbnail(url="attachment://master.png") # Message formatting breaks
embed.add_field(name="`def help`", value="Shows help message and list of commands", inline=True)
embed.add_field(name="aliases", value="None", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=True)
embed.add_field(name="`def hello`", value="Says hello", inline=True)
embed.add_field(name="aliases", value=f"{', '.join(j for j in ['`def ' + i + '`' for i in hello_aliases])}", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=True)
embed.add_field(name="`def meme`", value="Shares a meme from r/ProgrammerHumor", inline=True)
embed.add_field(name="aliases", value="`def mm`, `def maymay`", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=True)
embed.set_footer(text="Custom Meme Bot for NISER Coding Club Discord | Fork us on GitHub (https://github.com/sdgniser/DankCoderBot)", icon_url="attachment://master.png")
await ctx.channel.send(content=None, file=file, embed=embed)
@bot.command(name="hello", help="Says hello", aliases=hello_aliases)
async def hello(ctx):
await ctx.channel.send("Hello there!")
@bot.command(name="meme", help="Shares a meme from r/ProgrammerHumor", aliases=["maymay", "mm"])
async def meme(ctx):
await ctx.channel.send(get_meme())
@bot.event
async def on_error(event, *args, **kwargs):
with open('err.log', 'a') as f:
if event == 'on_message':
f.write(f'Unhandled message: {args[0]}\n')
else:
raise
@bot.event
async def on_message(message):
# Prefix == "def"
msg = message.content
# if msg.lower().startswith('recursion'):
# i = 0
# while i < 5:
# if random.random() < RECURSION_THRESHOLD:
# await message.channel.send("Recursion?!")
# i += 1
# await message.channel.send("Aw snap! I am written in Python. Guess I'll die ¯\_(ツ)_/¯.")
if message.author == bot.user:
return
if msg.lower().startswith('general kenobi'):
await message.channel.send("You are a bold one!")
if any(word in msg for word in spooky_triggers):
if random.random() < SPOOKINESS_THRESHOLD:
await message.channel.send("_It doesn't look like anything to me._"
)
if any(word in msg for word in trigger_words):
if random.random() < DANKNESS_THRESHOLD:
await message.channel.send(random.sample(dank_texts, 1)[0])
else:
await message.channel.send(get_meme())
await bot.process_commands(message)
resetCache()
keep_alive()
bot.run(TOKEN)