-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcole_bot.py
102 lines (72 loc) · 2.59 KB
/
cole_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
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv("dev.env")
guildID_key_name = ("test_" if os.getenv("mode") == "test" else "") + "guildID"
guildID = os.getenv(guildID_key_name)
if guildID is None:
print("Please set guildID in dev.env")
exit()
else:
guildID = int(guildID)
class MyBot(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self):
"""Called upon the READY event"""
print("Bot is ready.")
async def search_users(self, match):
# get the guild object
guild = self.get_guild(guildID)
# convert match to lowercase
prefix = match.lower()
user_match_count = 0
def select_user(user):
nonlocal user_match_count
# limit the number of matching users
res = user_match_count < 11 and user.name.lower().startswith(prefix)
if res:
user_match_count += 1
return res
# get matching users
matching_users = filter(
select_user, guild.members
)
# return list of user info objects
return [
{"username": user.name, "discord_user_id": str(user.id), "tag": f"#{user.discriminator}"}
for user in matching_users
]
async def user_id_to_username(self, user_id):
# get the guild object
guild = self.get_guild(guildID)
# get the member from the user_id
user = guild.get_member(int(user_id))
# return the user's name or none
if user:
return user.name
else:
return "Left server"
async def user_ids_to_usernames(self, user_ids):
# get the guild object
guild = self.get_guild(guildID)
# convert from user_ids to names
user_names = []
for user_id in user_ids:
# get the member from the user_id
user = guild.get_member(int(user_id))
# return the user's name or none
user_names.append(user.name if user else "Left Server")
return user_names
async def get_member_count(self):
# get the guild object
guild = self.get_guild(guildID)
# return the member count to the client
return guild.member_count
@commands.command()
async def info(self, ctx):
await ctx.send(f"Simple bot for sending data to dashboard")
if __name__ == "__main__":
my_bot = MyBot(command_prefix="random_word", intents=discord.Intents.all())
my_bot.run(os.getenv("bot_token"))