Skip to content

Commit

Permalink
Merge pull request #14 from The-Coder-Kishor/master
Browse files Browse the repository at this point in the history
[Update] Updated discord.py to 2.x along with other packages.
  • Loading branch information
ankith26 authored May 20, 2024
2 parents 9648efd + 5d1e3ec commit cebc04e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
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=""
64 changes: 44 additions & 20 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.reply(
f"<{BASE_URL}>\nSign in through our portal, and try again.", ephemeral=True
)


def get_config(server_id: str):
Expand Down Expand Up @@ -160,8 +167,9 @@ async def post_verification(ctx, user):
server_config = get_config(server_id)

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"
await ctx.reply(
"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.reply(
"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.reply(f"<@{user.id}> has been CAS-verified!")


@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 @@ -198,23 +209,25 @@ async def verify_user(ctx):
await send_link(ctx)
await asyncio.sleep(60)
else:
await ctx.send(
await ctx.reply(
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()
await ctx.send(
await ctx.reply(
f"Here are the server details:\n"
f"system: {uname.system}\n"
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 @@ -241,10 +254,13 @@ 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']}"
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.reply(
f"{identifier} is not registered with IIIT-CAS.", ephemeral=True
)


@query.error
Expand All @@ -253,10 +269,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.reply("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 @@ -272,10 +288,13 @@ 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']}"
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.reply(
f"{identifier} is not registered with IIIT-CAS.", ephemeral=True
)


@roll.error
Expand All @@ -284,7 +303,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 +329,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

0 comments on commit cebc04e

Please sign in to comment.