Skip to content

Commit 9e38de6

Browse files
TheGigaBobDotComLulalaby
authored
Changed styling for most of the examples. (#1566)
Co-authored-by: BobDotCom <[email protected]> Co-authored-by: Lala Sabathil <[email protected]>
1 parent f0e2027 commit 9e38de6

File tree

5 files changed

+189
-173
lines changed

5 files changed

+189
-173
lines changed

examples/deleted.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
# This example requires the `message_content` privileged intent for access to message content.
2-
31
import discord
42

3+
intents = discord.Intents.default()
4+
intents.message_content = True # < This may give you `read-only` warning, just ignore it.
5+
# This intent requires "Message Content Intent" to be enabled at https://discord.com/developers
6+
57

6-
class MyClient(discord.Client):
7-
async def on_ready(self):
8-
print(f"Logged in as {self.user} (ID: {self.user.id})")
9-
print("------")
8+
bot = discord.Bot(intents=intents)
109

11-
async def on_message(self, message: discord.Message):
12-
if message.content.startswith("!deleteme"):
13-
msg = await message.channel.send("I will delete myself now...")
14-
await msg.delete()
1510

16-
# This also works:
17-
await message.channel.send("Goodbye in 3 seconds...", delete_after=3.0)
11+
@bot.event
12+
async def on_ready():
13+
print('Ready!')
1814

19-
async def on_message_delete(self, message: discord.Message):
20-
msg = f"{message.author} has deleted the message: {message.content}"
21-
await message.channel.send(msg)
2215

16+
@bot.event
17+
async def on_message(message: discord.Message):
18+
if message.content.startswith("!deleteme"):
19+
msg = await message.channel.send("I will delete myself now...")
20+
await msg.delete()
2321

24-
intents = discord.Intents.default()
25-
intents.message_content = True
22+
# This also works:
23+
await message.channel.send("Goodbye in 3 seconds...", delete_after=3.0)
24+
25+
26+
@bot.event
27+
async def on_message_delete(message: discord.Message):
28+
msg = f"{message.author} has deleted the message: {message.content}"
29+
await message.channel.send(msg)
30+
31+
32+
bot.run("TOKEN")
2633

27-
client = MyClient(intents=intents)
28-
client.run("TOKEN")

examples/edits.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
# This example requires the `message_content` privileged intent for access to message content.
2-
1+
import discord
32
import asyncio
43

5-
import discord
4+
intents = discord.Intents.default()
5+
intents.message_content = True # < This may give you `read-only` warning, just ignore it.
6+
# This intent requires "Message Content Intent" to be enabled at https://discord.com/developers
67

78

8-
class MyClient(discord.Client):
9-
async def on_ready(self):
10-
print(f"Logged in as {self.user} (ID: {self.user.id})")
11-
print("------")
9+
bot = discord.Bot(intents=intents)
1210

13-
async def on_message(self, message: discord.Message):
14-
if message.content.startswith("!editme"):
15-
msg = await message.channel.send("10")
16-
await asyncio.sleep(3.0)
17-
await msg.edit(content="40")
1811

19-
async def on_message_edit(self, before: discord.Message, after: discord.Message):
20-
msg = f"**{before.author}** edited their message:\n{before.content} -> {after.content}"
21-
await before.channel.send(msg)
12+
@bot.event
13+
async def on_ready():
14+
print('Ready!')
2215

2316

24-
intents = discord.Intents.default()
25-
intents.message_content = True
17+
@bot.event
18+
async def on_message(message: discord.Message):
19+
if message.content.startswith("!editme"):
20+
msg = await message.channel.send("10")
21+
await asyncio.sleep(3.0)
22+
await msg.edit(content="40")
23+
24+
25+
@bot.event
26+
async def on_message_edit(before: discord.Message, after: discord.Message):
27+
msg = f"**{before.author}** edited their message:\n{before.content} -> {after.content}"
28+
await before.channel.send(msg)
29+
2630

27-
client = MyClient(intents=intents)
28-
client.run("TOKEN")
31+
bot.run("TOKEN")

examples/reaction_roles.py

Lines changed: 91 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,94 @@
1-
# This example requires the 'members' privileged intent for access to .get_member.
2-
31
import discord
4-
5-
6-
class MyClient(discord.Client):
7-
def __init__(self, *args, **kwargs):
8-
super().__init__(*args, **kwargs)
9-
10-
self.role_message_id = 0 # ID of the message that can be reacted to for adding/removing a role.
11-
self.emoji_to_role = {
12-
discord.PartialEmoji(name="🔴"): 0, # ID of the role associated with unicode emoji '🔴'.
13-
discord.PartialEmoji(name="🟡"): 0, # ID of the role associated with unicode emoji '🟡'.
14-
discord.PartialEmoji(name="green", id=0): 0, # ID of the role associated with a partial emoji's ID.
15-
}
16-
17-
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
18-
"""Gives a role based on a reaction emoji."""
19-
# Make sure that the message the user is reacting to is the one we care about.
20-
if payload.message_id != self.role_message_id:
21-
return
22-
23-
guild = self.get_guild(payload.guild_id)
24-
if guild is None:
25-
# Make sure we're still in the guild, and it's cached.
26-
return
27-
28-
try:
29-
role_id = self.emoji_to_role[payload.emoji]
30-
except KeyError:
31-
# If the emoji isn't the one we care about then exit as well.
32-
return
33-
34-
role = guild.get_role(role_id)
35-
if role is None:
36-
# Make sure the role still exists and is valid.
37-
return
38-
39-
try:
40-
# Finally, add the role.
41-
await payload.member.add_roles(role)
42-
except discord.HTTPException:
43-
# If we want to do something in case of errors we'd do it here.
44-
pass
45-
46-
async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent):
47-
"""Removes a role based on a reaction emoji."""
48-
# Make sure that the message the user is reacting to is the one we care about.
49-
if payload.message_id != self.role_message_id:
50-
return
51-
52-
guild = self.get_guild(payload.guild_id)
53-
if guild is None:
54-
# Make sure we're still in the guild, and it's cached.
55-
return
56-
57-
try:
58-
role_id = self.emoji_to_role[payload.emoji]
59-
except KeyError:
60-
# If the emoji isn't the one we care about then exit as well.
61-
return
62-
63-
role = guild.get_role(role_id)
64-
if role is None:
65-
# Make sure the role still exists and is valid.
66-
return
67-
68-
# The payload for `on_raw_reaction_remove` does not provide `.member`
69-
# so we must get the member ourselves from the payload's `.user_id`.
70-
member = guild.get_member(payload.user_id)
71-
if member is None:
72-
# Make sure the member still exists and is valid.
73-
return
74-
75-
try:
76-
# Finally, remove the role.
77-
await member.remove_roles(role)
78-
except discord.HTTPException:
79-
# If we want to do something in case of errors we'd do it here.
80-
pass
81-
2+
import asyncio
823

834
intents = discord.Intents.default()
84-
intents.members = True
85-
86-
client = MyClient(intents=intents)
87-
client.run("TOKEN")
5+
intents.members = True # < This may give you `read-only` warning, just ignore it.
6+
# This intent requires "Server Members Intent" to be enabled at https://discord.com/developers
7+
8+
9+
bot = discord.Bot(intents=intents)
10+
11+
role_message_id = 0 # ID of the message that can be reacted to for adding/removing a role.
12+
13+
emoji_to_role = {
14+
discord.PartialEmoji(name="🔴"): 0, # ID of the role associated with unicode emoji '🔴'.
15+
discord.PartialEmoji(name="🟡"): 0, # ID of the role associated with unicode emoji '🟡'.
16+
discord.PartialEmoji(name="green", id=0): 0, # ID of the role associated with a partial emoji's ID.
17+
}
18+
19+
20+
@bot.event
21+
async def on_ready():
22+
print('Ready!')
23+
24+
25+
@bot.event
26+
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent):
27+
"""Gives a role based on a reaction emoji."""
28+
# Make sure that the message the user is reacting to is the one we care about.
29+
if payload.message_id != role_message_id:
30+
return
31+
32+
guild = bot.get_guild(payload.guild_id)
33+
if guild is None:
34+
# Make sure we're still in the guild, and it's cached.
35+
return
36+
37+
try:
38+
role_id = emoji_to_role[payload.emoji]
39+
except KeyError:
40+
# If the emoji isn't the one we care about then exit as well.
41+
return
42+
43+
role = guild.get_role(role_id)
44+
if role is None:
45+
# Make sure the role still exists and is valid.
46+
return
47+
48+
try:
49+
# Finally, add the role.
50+
await payload.member.add_roles(role)
51+
except discord.HTTPException:
52+
# If we want to do something in case of errors we'd do it here.
53+
pass
54+
55+
56+
@bot.event
57+
async def on_raw_reaction_remove(payload: discord.RawReactionActionEvent):
58+
"""Removes a role based on a reaction emoji."""
59+
# Make sure that the message the user is reacting to is the one we care about.
60+
if payload.message_id != role_message_id:
61+
return
62+
63+
guild = bot.get_guild(payload.guild_id)
64+
if guild is None:
65+
# Make sure we're still in the guild, and it's cached.
66+
return
67+
68+
try:
69+
role_id = emoji_to_role[payload.emoji]
70+
except KeyError:
71+
# If the emoji isn't the one we care about then exit as well.
72+
return
73+
74+
role = guild.get_role(role_id)
75+
if role is None:
76+
# Make sure the role still exists and is valid.
77+
return
78+
79+
# The payload for `on_raw_reaction_remove` does not provide `.member`
80+
# so we must get the member ourselves from the payload's `.user_id`.
81+
member = guild.get_member(payload.user_id)
82+
if member is None:
83+
# Make sure the member still exists and is valid.
84+
return
85+
86+
try:
87+
# Finally, remove the role.
88+
await member.remove_roles(role)
89+
except discord.HTTPException:
90+
# If we want to do something in case of errors we'd do it here.
91+
pass
92+
93+
94+
bot.run("TOKEN")

examples/reply.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
# This example requires the `message_content` privileged intent for access to message content.
2-
31
import discord
42

3+
intents = discord.Intents.default()
4+
intents.message_content = True # < This may give you `read-only` warning, just ignore it.
5+
# This intent requires "Message Content Intent" to be enabled at https://discord.com/developers
56

6-
class MyClient(discord.Client):
7-
async def on_ready(self):
8-
print(f"Logged in as {self.user} (ID: {self.user.id})")
9-
print("------")
107

11-
async def on_message(self, message: discord.Message):
12-
# Make sure we won't be replying to ourselves.
13-
if message.author.id == self.user.id:
14-
return
8+
bot = discord.Bot(intents=intents)
159

16-
if message.content.startswith("!hello"):
17-
await message.reply("Hello!", mention_author=True)
1810

11+
@bot.event
12+
async def on_ready():
13+
print('Ready!')
14+
15+
16+
@bot.event
17+
async def on_message(message: discord.Message):
18+
# Make sure we won't be replying to ourselves.
19+
if message.author.id == bot.user.id:
20+
return
21+
22+
if message.content.startswith("!hello"):
23+
await message.reply("Hello!", mention_author=True)
1924

20-
intents = discord.Intents.default()
21-
intents.message_content = True
2225

23-
client = MyClient(intents=intents)
24-
client.run("TOKEN")
26+
bot.run("TOKEN")

0 commit comments

Comments
 (0)