Tech With Tim Logo
Go back

Sending and Receiving Messages

Click here for Discord.py Rewrite Documentation

Getting the Server ID

Now that we've created our bot and added it to our server it is likely that we would like it to give us some information about that specific server. Maybe how many member there are, how many people are online or the channels that exist. To be able to gather this information we need what's known as the server ID.

To get the server ID we need to enable developer mode on discord. To do this go to User Setting > Appearance and turn Developer Mode on. kuva_2023-04-23_184819135.png

Then you need to go to your Server Settings > Widget and copy the server ID. kuva_2023-04-23_184843845.png

Getting Number of Members

Now that we need have the server ID we have to use it to reference the server. We will declare the variable ID to be the following.

id = client.get_guild(YOUR SERVER ID HERE)
# you do NOT need to surround the ID with quotations

To get the number of members in our server we can do the following.

@client.event
async def on_message(message):
    id = client.get_guild(ID)

    if message.content.find("!hello") != -1:
        await message.channel.send("Hi") 
    elif message.content == "!users":
        await message.channel.send(f"""# of Members: {id.member_count}""") 
# We can use id.member_count

Welcoming Users

Something that we may want to do is welcome users when they first join the server. To do this we need to create a new event.

@client.event
async def on_member_join(member):
    for channel in member.guild.channels:
        if str(channel) == "general": # We check to make sure we are sending the message in the general channel
            await channel.send_message(f"""Welcome to the server {member.mention}""")

Restricting Channels

We may want to restrict the channels in which we can use bot commands. To do this we need to make a list of valid channels and check if the user typed in that channel.

@client.event
async def on_message(message):
    id = client.get_guild(ID)
    channels = ["commands"]

    if str(message.channel) in channels: # Check if in correct channel
        if message.content.find("!hello") != -1:
            await message.channel.send("Hi")
        elif message.content == "!users":
            await message.channel.send(f"""# of Members: {id.member_count}""")

Restricting Users

Similarly to above we may only want certain users to be able able to execute certain commands.

@client.event
async def on_message(message):
    id = client.get_guild(ID)
    channels = ["commands"]
    valid_users = ["Tim#9298"] # Only users in this list can use commands

    if str(message.channel) in channels and str(message.author) in valid_users:
        if message.content.find("!hello") != -1:
            await message.channel.send("Hi")
        elif message.content == "!users":
            await message.channel.send(f"""# of Members: {id.member_count}""")

Full Code

import discord

client = discord.Client()


@client.event
async def on_member_join(member):
    for channel in member.guild.channels:
        if str(channel) == "general":
            await channel.send_message(f"""Welcome to the server {member.mention}""")


@client.event
async def on_message(message):
    id = client.get_guild(ID)
    channels = ["commands"]
    valid_users = ["Ti#9298"]

    if str(message.channel) in channels and str(message.author) in valid_users:
        if message.content.find("!hello") != -1:
            await message.channel.send("Hi") 
        elif message.content == "!users":
            await message.channel.send(f"""# of Members: {id.member_count}""")

client.run(token)
Design & Development by Ibezio Logo