Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Update] Updated discord.py to 2.x along with other packages. #14

Merged
merged 11 commits into from
May 20, 2024
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ DISCORD_TOKEN=""
CAS_LINK="https://cas.my-org.com/login"
DISCORD_CLIENT_ID=""
DISCORD_SECRET=""
SECRET=""
SECRET=""
58 changes: 39 additions & 19 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from config_verification import read_and_validate_config

load_dotenv()

TOKEN = os.getenv("DISCORD_TOKEN")
MONGO_DATABASE = os.getenv("MONGO_DATABASE")
MONGO_URI = os.getenv("MONGO_URI")
Expand All @@ -55,7 +56,11 @@
BASE_URL = f"{PROTOCOL}://{HOST}{_PORT_AS_SUFFIX}{SUBPATH}"
SERVER_CONFIG = ConfigParser()

bot = commands.Bot(command_prefix=".")
intent = discord.Intents.default()
intent.message_content = True
bot = commands.Bot(command_prefix=".", intents=intent)
# to get message privelege

db: database.Database = None # assigned in main function


Expand All @@ -82,7 +87,9 @@ def get_realname_from_discordid(user_id):

async def send_link(ctx):
"""Sends the base url for users to reattempt sign-in."""
await ctx.send(f"<{BASE_URL}>\nSign in through our portal, and try again.")
await ctx.send(
f"<{BASE_URL}>\nSign in through our portal, and try again.", ephemeral=True
)


def get_config(server_id: str):
Expand Down Expand Up @@ -161,7 +168,8 @@ async def post_verification(ctx, user):

if server_config is None:
await ctx.send(
"This server is not authorized to work with CAS-bot. Read the instructions to invite the bot in the project README"
"This server is not authorized to work with CAS-bot. Read the instructions to invite the bot in the project README",
ephemeral=True,
)
await ctx.guild.leave()
return
Expand All @@ -172,12 +180,15 @@ async def post_verification(ctx, user):
try:
await set_nickname(user, server_config)
except discord.DiscordException:
await ctx.send("Bot should have a role higher than you to change your nickname")
await ctx.send(
"Bot should have a role higher than you to change your nickname",
ephemeral=True,
)

await ctx.send(f"<@{user.id}> has been CAS-verified!")
await ctx.send(f"<@{user.id}> has been CAS-verified!", ephemeral=True)
ankith26 marked this conversation as resolved.
Show resolved Hide resolved


@bot.command(name="verify")
@bot.hybrid_command(name="verify")
async def verify_user(ctx):
"""
Runs when the user types `.verify` in the server. First tries to find the user in the DB.
Expand All @@ -200,11 +211,12 @@ async def verify_user(ctx):
else:
await ctx.send(
f"Sorry <@{user_id}>, could not auto-detect your verification. \
Please run `.verify` again."
Please run `.verify` again.",
ephemeral=True,
)


@bot.command(name="backend_info")
@bot.hybrid_command(name="backend_info")
async def backend_info(ctx):
"""For debugging server info; sends details of the server."""
uname = platform.uname()
Expand All @@ -214,7 +226,8 @@ async def backend_info(ctx):
f"node: {uname.node}\n"
f"release: {uname.release}\n"
f"version: {uname.version}\n"
f"machine: {uname.machine}"
f"machine: {uname.machine}",
ephemeral=True,
)


Expand All @@ -227,7 +240,7 @@ def is_academic(ctx: commands.Context):
return server_config.get("is_academic", False)


@bot.command(name="query")
@bot.hybrid_command(name="query")
@commands.check(is_academic)
async def query(
ctx: commands.Context,
Expand All @@ -240,11 +253,12 @@ async def query(
"""
user = db.users.find_one({"discordId": str(identifier.id)})
if user:
await ctx.reply(
f"Name: {user['name']}\nEmail: {user['email']}\nRoll Number: {user['rollno']}"
await ctx.send(
f"Name: {user['name']}\nEmail: {user['email']}\nRoll Number: {user['rollno']}",
ephemeral=True,
)
else:
await ctx.reply(f"{identifier} is not registered with IIIT-CAS.")
await ctx.send(f"{identifier} is not registered with IIIT-CAS.", ephemeral=True)


@query.error
Expand All @@ -253,10 +267,10 @@ async def query_error(ctx, error):
For the `query` command, if the server is not academic, replies with error message.
"""
if isinstance(error, commands.CheckFailure):
await ctx.reply("This server is not for academic purposes.")
await ctx.send("This server is not for academic purposes.", ephemeral=True)


@bot.command(name="roll")
@bot.hybrid_command(name="roll")
@commands.check(is_academic)
async def roll(
ctx: commands.Context,
Expand All @@ -271,11 +285,12 @@ async def roll(
"""
user = db.users.find_one({"rollno": str(identifier)})
if user:
await ctx.reply(
f"Name: {user['name']}\nEmail: {user['email']}\nRoll Number: {user['rollno']}"
await ctx.send(
f"Name: {user['name']}\nEmail: {user['email']}\nRoll Number: {user['rollno']}",
ephemeral=True,
)
else:
await ctx.reply(f"{identifier} is not registered with IIIT-CAS.")
await ctx.send(f"{identifier} is not registered with IIIT-CAS.", ephemeral=True)
ankith26 marked this conversation as resolved.
Show resolved Hide resolved


@roll.error
Expand All @@ -284,7 +299,7 @@ async def roll_error(ctx, error):
For the `roll` command, if the server is not academic, replies with error message.
"""
if isinstance(error, commands.CheckFailure):
await ctx.reply("This server is not for academic purposes.")
await ctx.reply("This server is not for academic purposes.", ephemeral=True)


@bot.event
Expand All @@ -310,6 +325,11 @@ async def on_guild_join(guild):
async def on_ready():
"""This is executed when the bot connects to a server."""
print(f"{bot.user.name} has connected to Discord!")
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} commands.")
except Exception as e:
print(e)


def main():
Expand Down
18 changes: 10 additions & 8 deletions bot/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
aiohttp==3.7.4.post0
async-timeout==3.0.1
aiohttp==3.9.5
aiosignal==1.3.1
async-timeout==4.0.3
attrs==23.2.0
chardet==4.0.0
discord.py==1.7.2
dnspython==1.16.0
idna==3.6
chardet==5.2.0
discord.py==2.3.2
dnspython==2.6.1
frozenlist==1.4.1
idna==3.7
multidict==6.0.5
pymongo==3.13.0
pymongo==4.7.2
python-dotenv==1.0.1
typing_extensions==4.10.0
typing_extensions==4.11.0
yarl==1.9.4