Browse Source

Examples now use Python 3.5. Remove echo.py example.

pull/77/head
Rapptz 9 years ago
parent
commit
2ebfbbe471
  1. 13
      examples/background_task.py
  2. 18
      examples/deleted.py
  3. 16
      examples/echo.py
  4. 20
      examples/edits.py
  5. 18
      examples/guessing_game.py
  6. 10
      examples/new_member.py
  7. 36
      examples/playlist.py
  8. 10
      examples/reply.py

13
examples/background_task.py

@ -3,18 +3,17 @@ import asyncio
client = discord.Client() client = discord.Client()
@asyncio.coroutine async def my_background_task():
def my_background_task(): await client.wait_until_ready()
yield from client.wait_until_ready()
counter = 0 counter = 0
channel = discord.Object(id='channel_id_here') channel = discord.Object(id='channel_id_here')
while not client.is_closed: while not client.is_closed:
counter += 1 counter += 1
yield from client.send_message(channel, counter) await client.send_message(channel, counter)
yield from asyncio.sleep(60) # task runs every 60 seconds await asyncio.sleep(60) # task runs every 60 seconds
@client.async_event @client.event
def on_ready(): async def on_ready():
print('Logged in as') print('Logged in as')
print(client.user.name) print(client.user.name)
print(client.user.id) print(client.user.id)

18
examples/deleted.py

@ -2,21 +2,21 @@ import discord
client = discord.Client() client = discord.Client()
@client.async_event @client.event
def on_ready(): async def on_ready():
print('Connected!') print('Connected!')
print('Username: ' + client.user.name) print('Username: ' + client.user.name)
print('ID: ' + client.user.id) print('ID: ' + client.user.id)
@client.async_event @client.event
def on_message(message): async def on_message(message):
if message.content.startswith('!deleteme'): if message.content.startswith('!deleteme'):
msg = yield from client.send_message(message.channel, 'I will delete myself now...') msg = await client.send_message(message.channel, 'I will delete myself now...')
yield from client.delete_message(msg) await client.delete_message(msg)
@client.async_event @client.event
def on_message_delete(message): async def on_message_delete(message):
fmt = '{0.author.name} has deleted the message:\n{0.content}' fmt = '{0.author.name} has deleted the message:\n{0.content}'
yield from client.send_message(message.channel, fmt.format(message)) await client.send_message(message.channel, fmt.format(message))
client.run('email', 'password') client.run('email', 'password')

16
examples/echo.py

@ -1,16 +0,0 @@
import discord
client = discord.Client()
@client.async_event
def on_ready():
print('Connected!')
print('Username: ' + client.user.name)
print('ID: ' + client.user.id)
@client.async_event
def on_message(message):
if message.author.id != client.user.id:
yield from client.send_message(message.channel, message.content)
client.run('email', 'password')

20
examples/edits.py

@ -3,22 +3,22 @@ import asyncio
client = discord.Client() client = discord.Client()
@client.async_event @client.event
def on_ready(): async def on_ready():
print('Connected!') print('Connected!')
print('Username: ' + client.user.name) print('Username: ' + client.user.name)
print('ID: ' + client.user.id) print('ID: ' + client.user.id)
@client.async_event @client.event
def on_message(message): async def on_message(message):
if message.content.startswith('!editme'): if message.content.startswith('!editme'):
msg = yield from client.send_message(message.author, '10') msg = await client.send_message(message.author, '10')
yield from asyncio.sleep(3) await asyncio.sleep(3)
yield from client.edit_message(msg, '40') await client.edit_message(msg, '40')
@client.async_event @client.event
def on_message_edit(before, after): async def on_message_edit(before, after):
fmt = '**{0.author}** edited their message:\n{1.content}' fmt = '**{0.author}** edited their message:\n{1.content}'
yield from client.send_message(after.channel, fmt.format(after, before)) await client.send_message(after.channel, fmt.format(after, before))
client.run('email', 'password') client.run('email', 'password')

18
examples/guessing_game.py

@ -3,32 +3,32 @@ import random
client = discord.Client() client = discord.Client()
@client.async_event @client.event
def on_message(message): async def on_message(message):
# we do not want the bot to reply to itself # we do not want the bot to reply to itself
if message.author == client.user: if message.author == client.user:
return return
if message.content.startswith('$guess'): if message.content.startswith('$guess'):
yield from client.send_message(message.channel, 'Guess a number between 1 to 10') await client.send_message(message.channel, 'Guess a number between 1 to 10')
def guess_check(m): def guess_check(m):
return m.content.isdigit() return m.content.isdigit()
guess = yield from client.wait_for_message(timeout=5.0, author=message.author, check=guess_check) guess = await client.wait_for_message(timeout=5.0, author=message.author, check=guess_check)
answer = random.randint(1, 10) answer = random.randint(1, 10)
if guess is None: if guess is None:
fmt = 'Sorry, you took too long. It was {}.' fmt = 'Sorry, you took too long. It was {}.'
yield from client.send_message(message.channel, fmt.format(answer)) await client.send_message(message.channel, fmt.format(answer))
return return
if int(guess.content) == answer: if int(guess.content) == answer:
yield from client.send_message(message.channel, 'You are right!') await client.send_message(message.channel, 'You are right!')
else: else:
yield from client.send_message(message.channel, 'Sorry. It is actually {}.'.format(answer)) await client.send_message(message.channel, 'Sorry. It is actually {}.'.format(answer))
@client.async_event @client.event
def on_ready(): async def on_ready():
print('Logged in as') print('Logged in as')
print(client.user.name) print(client.user.name)
print(client.user.id) print(client.user.id)

10
examples/new_member.py

@ -2,14 +2,14 @@ import discord
client = discord.Client() client = discord.Client()
@client.async_event @client.event
def on_member_join(member): async def on_member_join(member):
server = member.server server = member.server
fmt = 'Welcome {0.mention} to {1.name}!' fmt = 'Welcome {0.mention} to {1.name}!'
yield from client.send_message(server, fmt.format(member, server)) await client.send_message(server, fmt.format(member, server))
@client.async_event @client.event
def on_ready(): async def on_ready():
print('Logged in as') print('Logged in as')
print(client.user.name) print(client.user.name)
print(client.user.id) print(client.user.id)

36
examples/playlist.py

@ -5,7 +5,7 @@ if not discord.opus.is_loaded():
# the 'opus' library here is opus.dll on windows # the 'opus' library here is opus.dll on windows
# or libopus.so on linux in the current directory # or libopus.so on linux in the current directory
# you should replace this with the location the # you should replace this with the location the
# opus library is located in. # opus library is located in and with the proper filename.
discord.opus.load_opus('opus') discord.opus.load_opus('opus')
class VoiceEntry: class VoiceEntry:
@ -32,69 +32,67 @@ class Bot(discord.Client):
def is_playing(self): def is_playing(self):
return self.player is not None and self.player.is_playing() return self.player is not None and self.player.is_playing()
@asyncio.coroutine async def on_message(self, message):
def on_message(self, message):
if message.author == self.user: if message.author == self.user:
return return
if message.channel.is_private: if message.channel.is_private:
yield from self.send_message(message.channel, 'You cannot use this bot in private messages.') await self.send_message(message.channel, 'You cannot use this bot in private messages.')
if message.content.startswith('$join'): if message.content.startswith('$join'):
if self.is_voice_connected(): if self.is_voice_connected():
yield from self.send_message(message.channel, 'Already connected to a voice channel') await self.send_message(message.channel, 'Already connected to a voice channel')
channel_name = message.content[5:].strip() channel_name = message.content[5:].strip()
check = lambda c: c.name == channel_name and c.type == discord.ChannelType.voice check = lambda c: c.name == channel_name and c.type == discord.ChannelType.voice
channel = discord.utils.find(check, message.server.channels) channel = discord.utils.find(check, message.server.channels)
if channel is None: if channel is None:
yield from self.send_message(message.channel, 'Cannot find a voice channel by that name.') await self.send_message(message.channel, 'Cannot find a voice channel by that name.')
yield from self.join_voice_channel(channel) await self.join_voice_channel(channel)
self.starter = message.author self.starter = message.author
elif message.content.startswith('$leave'): elif message.content.startswith('$leave'):
if not self.can_control_song(message.author): if not self.can_control_song(message.author):
return return
self.starter = None self.starter = None
yield from self.voice.disconnect() await self.voice.disconnect()
elif message.content.startswith('$pause'): elif message.content.startswith('$pause'):
if not self.can_control_song(message.author): if not self.can_control_song(message.author):
fmt = 'Only the requester ({0.current.requester}) can control this song' fmt = 'Only the requester ({0.current.requester}) can control this song'
yield from self.send_message(message.channel, fmt.format(self)) await self.send_message(message.channel, fmt.format(self))
if self.player.is_playing(): if self.player.is_playing():
self.player.pause() self.player.pause()
elif message.content.startswith('$resume'): elif message.content.startswith('$resume'):
if not self.can_control_song(message.author): if not self.can_control_song(message.author):
fmt = 'Only the requester ({0.current.requester}) can control this song' fmt = 'Only the requester ({0.current.requester}) can control this song'
yield from self.send_message(message.channel, fmt.format(self)) await self.send_message(message.channel, fmt.format(self))
if self.player is not None and not self.is_playing(): if self.player is not None and not self.is_playing():
self.player.resume() self.player.resume()
elif message.content.startswith('$next'): elif message.content.startswith('$next'):
filename = message.content[5:].strip() filename = message.content[5:].strip()
yield from self.songs.put(VoiceEntry(message, filename)) await self.songs.put(VoiceEntry(message, filename))
yield from self.send_message(message.channel, 'Successfully registered {}'.format(filename)) await self.send_message(message.channel, 'Successfully registered {}'.format(filename))
elif message.content.startswith('$play'): elif message.content.startswith('$play'):
if self.player is not None and self.player.is_playing(): if self.player is not None and self.player.is_playing():
yield from self.send_message(message.channel, 'Already playing a song') await self.send_message(message.channel, 'Already playing a song')
return return
while True: while True:
if not self.is_voice_connected(): if not self.is_voice_connected():
yield from self.send_message(message.channel, 'Not connected to a voice channel') await self.send_message(message.channel, 'Not connected to a voice channel')
return return
self.play_next_song.clear() self.play_next_song.clear()
self.current = yield from self.songs.get() self.current = await self.songs.get()
self.player = self.voice.create_ffmpeg_player(self.current.song, after=self.toggle_next_song) self.player = self.voice.create_ffmpeg_player(self.current.song, after=self.toggle_next_song)
self.player.start() self.player.start()
fmt = 'Playing song "{0.song}" from {0.requester}' fmt = 'Playing song "{0.song}" from {0.requester}'
yield from self.send_message(self.current.channel, fmt.format(self.current)) await self.send_message(self.current.channel, fmt.format(self.current))
yield from self.play_next_song.wait() await self.play_next_song.wait()
@asyncio.coroutine async def on_ready(self):
def on_ready(self):
print('Logged in as') print('Logged in as')
print(self.user.name) print(self.user.name)
print(self.user.id) print(self.user.id)

10
examples/reply.py

@ -2,18 +2,18 @@ import discord
client = discord.Client() client = discord.Client()
@client.async_event @client.event
def on_message(message): async def on_message(message):
# we do not want the bot to reply to itself # we do not want the bot to reply to itself
if message.author == client.user: if message.author == client.user:
return return
if message.content.startswith('!hello'): if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message) msg = 'Hello {0.author.mention}'.format(message)
yield from client.send_message(message.channel, msg) await client.send_message(message.channel, msg)
@client.async_event @client.event
def on_ready(): async def on_ready():
print('Logged in as') print('Logged in as')
print(client.user.name) print(client.user.name)
print(client.user.id) print(client.user.id)

Loading…
Cancel
Save