-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbot.py
More file actions
122 lines (103 loc) · 4.04 KB
/
bot.py
File metadata and controls
122 lines (103 loc) · 4.04 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
# bot.py
# Copyright (c) 2021 War-Keeper
import os
import discord
from discord import Intents
from discord.ext.commands import Bot
from dotenv import load_dotenv
from discord.ext.commands import Bot, has_permissions, CheckFailure
# ----------------------------------------------------------------------------------------------
# Initializes the discord bot with a unique TOKEN and joins the bot to a server provided by the
# GUILD token. Handles bot shutdown and error events
# ----------------------------------------------------------------------------------------------
# Load the environment
load_dotenv()
# Get the token for our bot
TOKEN = os.getenv("TOKEN")
# Get the token for our discord server
GUILD = os.getenv("GUILD")
UNVERIFIED_ROLE_NAME = os.getenv("UNVERIFIED_ROLE_NAME")
# Set the bots intents to all
intents = Intents.all()
# Set all bot commands to begin with $
bot = Bot(intents=intents, command_prefix="$")
# ------------------------------------------------------------------------------------------------------------------
# Function: on_ready()
# Description: Activates when the bot starts, prints the name of the server it joins and the names of all members
# of that server
# Inputs:
# -
# Outputs:
# -
# ------------------------------------------------------------------------------------------------------------------
@bot.event
async def on_ready():
guild = discord.utils.get(bot.guilds, name=GUILD)
print(
f"{bot.user} is connected to the following guild:\n"
f"{guild.name}(id: {guild.id})"
)
members = "\n -".join([member.name for member in guild.members])
print(f"Guild Members:\n - {members}")
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
bot.load_extension(f"cogs.{filename[:-3]}")
bot.load_extension("jishaku")
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching, name="Over This Server"
)
)
print("READY!")
# ------------------------------------------------------------------------------------------
# Function: on_member_join(member)
# Description: Handles on_member_join events, DMs the user and asks for verification through newComer.py
# Inputs:
# - member: used to add member to the knowledge of the bot
# Outputs:
# -
# ------------------------------------------------------------------------------------------
@bot.event
async def on_member_join(member):
unverified = discord.utils.get(
member.guild.roles, name=UNVERIFIED_ROLE_NAME
) # finds the unverified role in the guild
await member.add_roles(unverified) # assigns the unverified role to the new member
await member.send("Hello " + member.name + "!")
await member.send(
"Verify yourself before getting started! \n To use the verify command, do: $verify <your_full_name> \n \
( For example: $verify Jane Doe )")
# ------------------------------------------------
# Function: on_error(event, *args, **kwargs)
# Description: Handles bot errors, prints errors to a log file
# Inputs:
# - member: event of the error
# - *args: any arguments that come with error
# - **kwargs: other args
# Outputs:
# -
# ------------------------------------------------
@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
# ----------------------------------
# Function: on_member_join(member)
# Description: Command for shutting down the bot
# Inputs:
# - ctx: used to access the values passed through the current context
# Outputs:
# -
# ----------------------------------
@bot.command(name="shutdown", help="Shuts down the bot, only usable by the owner")
@has_permissions(administrator=True)
async def shutdown(ctx):
await ctx.send('Shutting Down bot')
print("Bot closed successfully")
ctx.bot.logout()
exit()
# Starts the bot with the current token
bot.run(TOKEN)