-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
170 lines (154 loc) Β· 7.34 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
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
import os
import json
import secrets
import discord
from discord import app_commands
import asyncio
# https://discordpy.readthedocs.io/en/stable/api.html
if "DISCORD_GUILD_ID" in os.environ:
MY_GUILD = discord.Object(id=int(os.environ["DISCORD_GUILD_ID"].strip()))
else:
MY_GUILD = discord.Object(id=int(open("guild.secret").read().strip()))
TYPING_BOT = int(os.environ.get("TYPING_BOT", "0"))
DISCORD_REACTION_MSGID = int(os.environ.get("DISCORD_REACTION_MSGID", "978574978914082836"))
class MyClient(discord.Client):
def __init__(self, *, intents: discord.Intents):
super().__init__(intents=intents)
# A CommandTree is a special type that holds all the application command
# state required to make it work. This is a separate class because it
# allows all the extra state to be opt-in.
# Whenever you want to work with application commands, your tree is used
# to store and work with them.
# Note: When using commands.Bot instead of discord.Client, the bot will
# maintain its own tree instead.
self.tree = app_commands.CommandTree(self)
# In this basic example, we just synchronize the app commands to one guild.
# Instead of specifying a guild to every command, we copy over our global commands instead.
# By doing so, we don't have to wait up to an hour until they are shown to the end-user.
async def setup_hook(self):
# This copies the global commands over to your guild.
self.tree.copy_global_to(guild=MY_GUILD)
await self.tree.sync(guild=MY_GUILD)
async def on_ready(self):
self.load_reaction_counts()
print(f'Logged on as {self.user}!')
"""
async def on_reaction_add(reaction, user):
if reaction.message.id == DISCORD_REACTION_MSGID:
print(reaction)
print(user)
"""
async def on_raw_reaction_add(self, payload):
#message = await self.get_channel(payload.channel_id).fetch_message(payload.message_id)
if payload.message_id == DISCORD_REACTION_MSGID:
guild = self.get_guild(payload.guild_id)
member = payload.member # only available if reaction add & inside guild...
try:
if payload.emoji.name == "π»":
await member.add_roles(guild.get_role(389678983605911554))
elif payload.emoji.name == "π":
await member.add_roles(guild.get_role(911464059830927401))
elif payload.emoji.name == "β‘":
await member.add_roles(guild.get_role(1061715448531525732))
#elif payload.emoji.name == "π":
# await member.add_roles(guild.get_role(1230232895671500901))
except Exception as e:
print(payload)
print(e)
async def on_raw_reaction_remove(self, payload):
#message = await self.get_channel(payload.channel_id).fetch_message(payload.message_id)
if payload.message_id == DISCORD_REACTION_MSGID:
guild = self.get_guild(payload.guild_id)
member = guild.get_member(payload.user_id)
if member is None:
return
try:
if payload.emoji.name == "π»":
await member.remove_roles(guild.get_role(389678983605911554))
elif payload.emoji.name == "π":
await member.remove_roles(guild.get_role(911464059830927401))
elif payload.emoji.name == "β‘":
await member.remove_roles(guild.get_role(1061715448531525732))
#elif payload.emoji.name == "π":
# await member.remove_roles(guild.get_role(1230232895671500901))
except Exception as e:
print(payload)
print(e)
async def send_typing(self, user, channel):
if not TYPING_BOT or user.id == self.user.id:
return
#if not channel.permissions_for(self.user).send_messages:
# return
try:
async with channel.typing():
await asyncio.sleep(3.5)
except:
pass
def load_reaction_counts(self):
with open("reaction_counts.json", "r", encoding="utf-8") as f:
self.reaction_counts = json.load(f)
def save_reaction_counts(self):
with open("reaction_counts.json", "w", encoding="utf-8") as f:
json.dump(self.reaction_counts, f)
def inc_reaction_counts(self, user_id, emoji):
user_id = str(user_id)
if not user_id in self.reaction_counts:
self.reaction_counts[user_id] = {}
if not emoji in self.reaction_counts[user_id]:
self.reaction_counts[user_id][emoji] = 0
self.reaction_counts[user_id][emoji] += 1
self.save_reaction_counts()
async def do_reaction_counts(self, message, emoji):
await message.add_reaction(emoji)
self.inc_reaction_counts(message.author.id, emoji)
async def on_message(self, message):
if not message.author.bot and not (message.guild is None):
await asyncio.sleep(0.5)
if secrets.randbelow(100) == 0:
await self.do_reaction_counts(message, "π¦")
if secrets.randbelow(1000) == 0:
await self.do_reaction_counts(message, ":goldbat:663437353787850763")
if secrets.randbelow(100) == 0:
await self.do_reaction_counts(message, ":corgi:702360708641194005")
if secrets.randbelow(1000) == 0:
await self.do_reaction_counts(message, ":corgibutt:788514694691553300")
await self.send_typing(message.author, message.channel)
async def on_typing(self, channel, user, when):
await self.send_typing(user, channel)
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
intents.reactions = True
intents.typing = True
client = MyClient(intents=intents)
@client.tree.command()
async def owowhatisthis(interaction: discord.Interaction):
await interaction.response.send_message(f'you just lost the game', ephemeral=True)
@client.tree.command()
async def top15(interaction: discord.Interaction):
embed = discord.Embed(colour=0x7289DA)
total_reactions = 0
total_reactions_by_user = {}
for (user_id, user) in client.reaction_counts.items():
total_reactions_by_user[user_id] = 0
for (emoji, emoji_count) in user.items():
total_reactions += emoji_count
total_reactions_by_user[user_id] += emoji_count
embed.add_field(name="Total reactions:", value=str(total_reactions))
sorted_users = [(k, total_reactions_by_user[k]) for k in sorted(total_reactions_by_user, key=total_reactions_by_user.get, reverse=True)]
sorted_users = sorted_users[:15]
top15_embed = ''
for user in sorted_users:
top15_embed += f"**\u2022** {user[1]} <@{user[0]}>"
for (emoji, emoji_count) in client.reaction_counts[user[0]].items():
if ":" in emoji:
top15_embed += f" | {emoji_count}<{emoji}>"
else:
top15_embed += f" | {emoji_count}{emoji}"
top15_embed += '\n'
embed.add_field(name="Top 15 users reacted to:", value=top15_embed)
await interaction.response.send_message(embed=embed)
if "DISCORD_TOKEN" in os.environ:
client.run(os.environ["DISCORD_TOKEN"].strip())
else:
client.run(open("token.secret").read().strip())