I will explain what I implemented while operating the server using Discord with code examples. The discord.py used is the rewrite version.
import discord
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run("")
The template looks like this. Write the process you want to implement between print ('------') and client.run ("").
# ?Hitting join will give you a title named member
@client.event
async def on_message(message):
if message.content == '?join' and len(message.author.roles) == 1:
role = discord.utils.get(message.guild.roles, name='member')
await message.author.add_roles(role)
reply = f'''Welcome{message.author.mention}Mr.
<#657524000993312768>Please read
<#657105501036740608>I would be grateful if you could write
'''
await message.channel.send(reply)
elif message.content == '+midnight':
role = discord.utils.get(message.guild.roles, name='midnight')
await message.author.add_roles(role)
elif message.content == '-midnight':
role = discord.utils.get(message.guild.roles, name='midnight')
await message.author.remove_roles(role)
elif message.content == '+novice':
role = discord.utils.get(message.guild.roles, name='novice')
await message.author.add_roles(role)
elif message.content == '-novice':
role = discord.utils.get(message.guild.roles, name='novice')
await message.author.remove_roles(role)
This process mentions the sender of the message and sends a reply when a? Join is written to the text channel. By sending a reply and adding a job title to a new member, you can prevent people who have not sent a? Join from seeing other channels on the server.
The only way to do this is to set the system message channel Prohibit @everyone to read the message + allow only sending, and prohibit @everyone from reading the message with the authority of other text channels and voice channels. At that time, the authority of the newly joined member's job title (@member here) makes it possible to read the message + only send it. By doing so, only the system message channel (#welcome here) will be displayed as a text channel for newly joined members. New members can send text to the system's message channel (#welcome), but they can't see the message history. (You can't see what you sent yourself. You can see it from the notification column only when you mention it.)
if message.content == '?join' and len(message.author.roles) == 1:
Determine if the message is a? Join and from the bot if the sender of the message does not have a job title (new people who enter the server only have the @everine job title, so they have one job title) I tried to send a message.
reply = f'''Welcome{message.author.mention}Mr.
<#657524000993312768>Please read
<#657105501036740608>I would be grateful if you could write
'''
Even if you write #Welcome in the text, it will not function as a hashtag (the color of the text will not change), so by writing <#text channel ID> in the text of the message you want to send, you can create a character string with a hashtag function. You can send it.
await message.channel.send(reply)
elif message.content == '+midnight':
role = discord.utils.get(message.guild.roles, name='midnight')
await message.author.add_roles(role)
elif message.content == '-midnight':
role = discord.utils.get(message.guild.roles, name='midnight')
await message.author.remove_roles(role)
elif message.content == '+novice':
role = discord.utils.get(message.guild.roles, name='novice')
await message.author.add_roles(role)
elif message.content == '-novice':
role = discord.utils.get(message.guild.roles, name='novice')
await message.author.remove_roles(role)
This code part is for sending + midnight or + novice to give midnight, novice titles, or sending -midnight, -novice to remove it. Using the permission settings of the text channel above, I created a text channel that only shows members with the novice position.
# +If you hit poll, all members will be given poll positions.-Job title removal with poll
if message.content == '+poll':
for i in range(len(message.guild.members)):
if 'Rythm' in str(message.guild.members[i]) or 'DISBOARD' in str(message.guild.members[i]) or 'Poll Bot' in str(message.guild.members[i]):
print('YES')
else:
print('NO')
print(message.guild.members[i])
role = discord.utils.get(message.guild.roles, name='poll')
await message.guild.members[i].add_roles(role)
if message.content == '-poll':
for i in range(len(message.guild.members)):
if 'Rythm' in str(message.guild.members[i]) or 'DISBOARD' in str(message.guild.members[i]) or 'Poll Bot' in str(message.guild.members[i]):
print('YES')
else:
print('NO')
print(message.guild.members[i])
role = discord.utils.get(message.guild.roles, name='poll')
await message.guild.members[i].remove_roles(role)
When someone writes + poll, this process assigns the poll position to all members except the bots (Rythm, DISBOARD, Poll Bot). We have created a text channel that only displays the #questionnaire survey, so that only people with the poll position will see it during the poll. This allows you to hide the displayed text channels except when you need them, which simplifies the list of text channels.
if 'Rythm' in str(message.guild.members[i]) or 'DISBOARD' in str(message.guild.members[i]) or 'Poll Bot' in str(message.guild.members[i]):
Here, it is judged whether it is a bot by looking at whether the member's name contains specific characters (Rythm, DISBOARD, Poll Bot in this example).
role = discord.utils.get(message.guild.roles, name='poll')
await message.guild.members[i].add_roles(role)
#When assigning a position to a member
await message.guild.members[i].remove_roles(role)
#When taking a position from a member
This process assigns or removes job titles to members.
# ?If you type clean, the log in the text channel you typed will disappear.
if message.content == '?clean':
if message.author.guild_permissions.administrator:
await message.channel.purge()
#await message.channel.send('Deleted the log in the channel')
elif message.channel.id == 657108629396389889:
print("YES")
print(message.channel.id)
await message.channel.purge()
else:
print("NO")
print(message.channel.id)
await message.channel.send('Administrator privileges are required to delete logs')
This is the code that deletes all conversation logs (up to 100 at a time) in the text channel that sent? Clean when the server administrator (only one of the crowns on each server) hits? Clean.
elif message.channel.id == 657108629396389889:
print("YES")
print(message.channel.id)
await message.channel.purge()
Only if the message was sent here for a specific channel (channel ID 657108629396389889), you can delete the conversation log even if you are not the server administrator.
Recommended Posts